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

后端 未结 7 1615
难免孤独
难免孤独 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条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 10:48

    I always use $. (current line number) to achieve this:

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    open my $fh, '<', 'myfile.txt' or die "$!\n";
    
    while (<$fh>) {
        next if $. < 2; # Skip first line
    
        # Do stuff with subsequent lines
    }
    

提交回复
热议问题