如何在Perl中将文件的行读入数组? [英] How can I read the lines of a file into an array in Perl?
本文介绍了如何在Perl中将文件的行读入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 test.txt 的文件,就像这样:
I have a file named test.txt that is like this:
测试
Foo
Bar
但是我想把每一行放在一个数组中,这样的行:
But I want to put each line in a array and print the lines like this:
line1 line2 line3
line1 line2 line3
<但是我怎么能这样做呢?
But how can I do this?
推荐答案
#!/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;
这篇关于如何在Perl中将文件的行读入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文