Perl how to read a line from a file handle that is an array element

后端 未结 3 1890
忘了有多久
忘了有多久 2021-01-22 17:42

My perl script takes any number of files, opens them, and reads one line from each. But it\'s not working. Here\'s my code.

#!/bin/perl
$numfile = scalar @ARGV         


        
3条回答
  •  攒了一身酷
    2021-01-22 18:38

    [Not an answer, but a comment that doesn't fit as a comment]

    Always use use strict; use warnings;. You are severely handicapping yourself without them.

    You don't need to use multiple handles since you never need more than one at a time.

    Some other cleanups:

    #!/bin/perl
    
    use strict;
    use warnings;
    
    my @first_lines;
    
    for my $qfn (@ARGV)
    {
        open(my $fh, '<', $qfn)
            or die("Can't open $qfn: $!\n");
    
        push @first_lines, scalar( <$fh> );
    }
    
    for my $i (0..$#first_lines)
    { 
        print  "$i => $first_lines[$i]"; 
    }
    

提交回复
热议问题