0

So, I need to get out the first line of file, and use its information for some purpose later. Also, I need to get the remaining lines from that same file and use information from first line to do something. I tried by the textbook this:

while (defined ($lines = <>))
    {
      #do something
    }

How to extract first line from this in Perl?.. I am new in programming, any advice would help.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • 1
    On reading the first line of a file: http://stackoverflow.com/questions/7028250/how-to-read-only-the-first-line-of-a-file – Kasper Munck May 02 '13 at 12:47

1 Answers1

8

Just read the first line before the loop starts:

my $first_line = <>;
while (my $line = <>) {
    # do whatever you like with $first_line and $line
}

If you want all the remaining lines in an array, no loop is needed at all:

my ($first_line, @lines) = <>;
choroba
  • 231,213
  • 25
  • 204
  • 289
  • I tried that, but the problem is that the first line also will be read by the while loop... I dont need to read it again.. – Gyrfalcon May 02 '13 at 12:28
  • @Gyrfalcon: It will not, unless you open the file again or `seek` to the beginning. – choroba May 02 '13 at 12:30