0

I have the txt file with the column value look like

85806534..85893402
49011742..49029143
114352846..114428174

I want to do the smartmatch. I put thses values into @array[0]

if ($line ~~$array[0]){do something here}

How can I make $array[0] as a number range?

if I split by .. then put the values into array [0] and array [1]

if ($line ~~[$array[0]..$array[1]){....}

this perl code is working for me.

Victor.H
  • 157
  • 1
  • 8
  • 1
    What you are doing is the way I'd do it. you could wrap the smart match inside a string eval like `if (eval "\$line ~~ $array[0]")` but you would have to be very sure that your data didn't hold any surprises. I'd also suspect the using eval this way would be slower – JGNI Oct 05 '18 at 08:45
  • 1
    Smartmatch is deprecated. – melpomene Oct 05 '18 at 09:04
  • 2
    Why use smartmatch (which is deprecated) when you can just use `$array[0] <= $line and $line <= $array[1])` ? – Corion Oct 05 '18 at 09:46
  • 2
    While smartmaching should be avoided, [it's experimental](https://stackoverflow.com/q/16927024/589924) (unreleased and subject to change or removal), not deprecated (intended to be removed) – ikegami Oct 05 '18 at 10:29

1 Answers1

4
$line ~~ [ do { my ($min, $max) = split(/\.\./, $array[0]); $min..$max } ]

The inefficiencies here are insane. Why do you want to create arrays with 10s of thousands of elements just to smartmatch? Even if smartmatch wasn't still flagged as experimental after all these years for being broken, you should be using the following instead:

my ($min, $max) = split(/\.\./, $array[0]);
$line >= $min && $line <= $max
ikegami
  • 367,544
  • 15
  • 269
  • 518