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

I have some code which reads from a file and then goes onto a foreach loop as so:
open (FILE, shift); my @file = <FILE>; foreach $a (@file) { ...etc... } close (FILE);
At the start of the code (not given here) I have a variable containing the first line in the file: $b = $file[0] and then the foreach loop. How can I make it so that once that first line is read and given to the variable, it is no longer read... as it interferes with my loop at the moment

Replies are listed 'Best First'.
Re: Read and skip a line
by Corion (Patriarch) on Apr 15, 2012 at 19:25 UTC

    Look at the functions for array manipulation: shift, pop, unshift and push - and splice.

    You might want to iterate over the file while reading, instead of loading it into memory as a whole. Then the next keyword could be of interest to you:

    my $first; while (<FILE>) { s/\s+$//; # strip newline and whitespace from end of line if( ! $first++) { print "The first line is '$_'\n"; next; }; print "Some other line is '$_'\n"; };
      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
        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)

Re: Read and skip a line
by jwkrahn (Abbot) on Apr 15, 2012 at 23:33 UTC
    open (FILE, shift); my @file = <FILE>; foreach $a (@file) { ...etc... } close (FILE);


    my $name = shift or die "usage: $0 filename\n"; open FILE, '<', $name or die "Cannot open '$name' because: $!"; my ( $firstline, @file ) = <FILE>; close FILE; foreach my $line ( @file ) { ...etc... }

    OR:

    my $name = shift or die "usage: $0 filename\n"; open FILE, '<', $name or die "Cannot open '$name' because: $!"; my $firstline = <FILE>; while ( my $line = <FILE> ) { ...etc... } close FILE;

    OR:

    my $name = shift or die "usage: $0 filename\n"; open FILE, '<', $name or die "Cannot open '$name' because: $!"; my $firstline; while ( my $line = <FILE> ) { if ( $. == 1 ) { $firstline = $line; next; } ...etc... } close FILE;