How can I read the lines of a file into an array in Perl?

后端 未结 7 2585
一向
一向 2021-02-20 12:22

I have a file named test.txt that is like this:

Test
Foo
Bar

But I want to put each line in a array and pri

7条回答
  •  情歌与酒
    2021-02-20 13:22

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    my @array;
    open(my $fh, "<", "test.txt")
        or die "Failed to open file: $!\n";
    while(<$fh>) { 
        chomp; 
        push @array, $_;
    } 
    close $fh;
    
    print join " ", @array;
    

提交回复
热议问题