http://qs321.pair.com?node_id=1153843

TJCooper has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to process multiple .txt files using a Perl one-liner:

perl -lane 'next unless $. >45; @array = split /[:,\/]+/, $F[2]; print if $array[1]/$array[2] >0.5 && $array[3] >2' input.txt > output.txt

This runs fine on all files individually, however when using:

perl -i.MOD -lane 'next unless $. >45; @array = split /[:,\/]+/, $F[9]; print if $array[3]/$array[4] >0.5 && $array[4] >5;' *.txt

It returns an error of:

Illegal division by zero at -e line 1, <> line 35885.

Why is that happening when each individual file runs to completion? Is there a better way to do this?

Replies are listed 'Best First'.
Re: Process multiple input files using a one-liner
by choroba (Cardinal) on Jan 28, 2016 at 12:35 UTC
    The problem is that $. is not reset to 1 when the diamond operator opens a new file. See eof on how to detect each file's end.
    $. = 1 if eof;
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      i think
      close ARGV if eof;
      is more adequate, me too i've resetted $. but is deprecable imho.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
        Works perfectly now, thanks!
Re: Process multiple input files using a one-liner
by Discipulus (Canon) on Jan 28, 2016 at 12:53 UTC
    Beside the fact of $. not resetting explained by choroba you can try wrapping the division into an eval {} block and if $@ is set print the line number and the value. i dunno how to print the current filename relative to the current value of the handle ARGV

    L*

    update i dunno why now runs with the proper close of ARGV : you have bad data before line 45? is the only reasonable possibility

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Lines 1-45 are header lines which do not contain the columnar data.