On Jan 27, Trina Espinoza said:

>So this may be wishful thinking, but I would be kicking myself later if I
>didn't ask. Is there a function in perl where you give the function exact
>line numbers and it would only read the data in the range of lines you
>gave it? My other alternative would be using a counter to find a start
>line and an end line in a file. Doable but painful. Let me know if there
>is any hope . . .

When you read a line from a filehandle, Perl stores the line number in $.,
so you can use that to your advantage:

  while (<FILE>) {
    if ($. >= $start and $. <= $end) {
      print;  # or do whatever
    }
  }

What's even better, though, is that there's another way to do that:

  while (<FILE>) {
    if ($. == $start .. $. == $end) {
      print;
    }
  }

and if those values ($start and $end) are constants that are hard-coded
into your program, you can just write it as:

  while (<FILE>) {
    if (10 .. 20) {
      print;  # displays lines 10 through 20
    }
  }

For more on the subject, please read 'perldoc perlop' and look for
'Range Operators'.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to