in reply to Re: Read and skip a line
in thread Read and skip a line

Hmm.. I couldn't get it to work, so I did a bit more on what I had, but now it prints out a certain line, by how many lines there are in total
#!/usr/bin/perl -w use strict; open (FILE, '<', shift); my @file = <FILE>; close (FILE); my $first = shift @file; foreach my $line (@file) { chomp $line; for (my $i = 1; $i <= $first; $i++) { print "Line $i: $line\n"; } }

Original content restored above by GrandFather

Ok sweet, I've got it to work your way, and I used the next and shift functions instead of giving a value through $line[0] :D

Replies are listed 'Best First'.
Re^3: Read and skip a line
by NetWallah (Canon) on Apr 15, 2012 at 21:34 UTC
    Most perl programmers would do this the way Corion indicated.

    Your logic is incorrect - here is a version using a "for loop" that you seem to want - , but coded in a more idomatic manner:

    use strict; use warnings; my $filename = shift; open my $file, "<", Filename or die "Cannot open $filename: $!"; chomp (my $lastrecord = <$file>); # Reads in the first record for (my $rec = 1; $rec <= $lastrecord and not eof($file); $rec++) { chomp(my $line = <$file>); print "Line $rec: $line\n"; };
    There is a small,subtle potential problem inherent in the code above - not corrected in order to maintain simplicity : the $line read in may be empty at EOF.

    UPDATE:Please do not delete code/comments after posting them - it makes responses like mine look out of context. It also prevents others from learning because context disappears.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)