Read a file into an array using Perl

后端 未结 4 1248
情书的邮戳
情书的邮戳 2020-12-31 06:25

I am currently reading a file and storing the data in an array named @lines. Then, I loop through this array using a for loop and inside the loop I

4条回答
  •  轮回少年
    2020-12-31 07:01

    You could also use the File::Slurp module, which is convenient.

    use strict;
    use warnings;
    use File::Slurp 'read_file';
    
    my $fname = shift or die 'filename!';
    my @lines = grep /fever/, read_file $fname; # grep with regular expression
    print @lines;
    

    If you're new to Perl, take a look at the map and grep operators, which are handy for processing lists.

    Also, take a look at the ack utility, which is a great replacement for find/grep. (Actually, it's a superior alternative.)

提交回复
热议问题