Best way to skip a header when reading in from a text file in Perl?

后端 未结 7 1638
难免孤独
难免孤独 2021-02-01 10:16

I\'m grabbing a few columns from a tab delineated file in Perl. The first line of the file is completely different from the other lines, so I\'d like to skip that line as fast a

7条回答
  •  Happy的楠姐
    2021-02-01 10:57

    You can read a file in a file handle and then can either use array or while loop to iterate over lines. for while loop, @Guru has the solution for you. for array, it would be as below:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    open (my $fh, '<','a.txt')  or die "cant open the file: $! \n";
    my @array = <$fh>;
    
    my $dummy = shift (@array);   << this is where the headers are stored.
    
    foreach (@array)
    {
       print $_."\n";
    }
    close ($fh);
    

提交回复
热议问题