Arrays perl:以数组形式输入多个文件

Arrays perl:以数组形式输入多个文件,arrays,perl,Arrays,Perl,我在不同的目录下有5个文件。我正在从所有文件中提取数据,并将其作为新文件 注意:将每个文件作为数组输入,并使用for循环为每个n个文件提取数据。我想让它成为单for循环,以获取文件并处理其余的文件 对于文件1,我使用 foreach (@file) { my @temp = split(/\t/, trim($_)); push(@output, $temp[0] . "\t" . $temp[1] . "\n"); } foreach(uniq(@output)) {

我在不同的目录下有5个文件。我正在从所有文件中提取数据,并将其作为新文件

注意:将每个文件作为数组输入,并使用for循环为每个n个文件提取数据。我想让它成为单for循环,以获取文件并处理其余的文件

对于文件1,我使用

foreach (@file)
{
    my @temp = split(/\t/, trim($_));
    push(@output, $temp[0] . "\t" . $temp[1] . "\n");
}

foreach(uniq(@output))
{
    print $OUTPUTFILE $_;
}

我这样做了五次,以处理五个文件。有谁能帮我简化一下吗?

只需将其包装在一个外部循环中,迭代所有五个文件:

for my $file ( @five_files ) {

    open my $fh, '<', $file or die "Unable to open $file: $!";
    my @file = <$fh>;

    foreach (@file) {
        my @temp = split(/\t/, trim($_));
        push(@output, $temp[0] . "\t" . $temp[1] . "\n");
    }

    foreach(uniq(@output)) {
        print $OUTPUTFILE $_;
    }
}

如果您通过使用join将@file数组展平来简化事情会怎么样。 然后你可以把它分开,然后处理这个列表。 例如:


但是,如果文件名中有空格,可能会出现问题

cat文件1文件2文件3 |使用perl做事情。pl@JohnC,我不想用猫。我需要在数组中这样做,你可以把代码放在一个循环中;单独提取前两行而不是前两列元素
my @temp = split /\t/, trim($_), 2;
push @output, @temp, "\n" ;
!/usr/bin/perl

my @file = ("file1\tfile3 ","file1\tfile3\tfile3 ","file2");  # Some test data.

my $in = join "\t", @file;  # Make one string.
my @temp = split(" ", $in); # Split it on whitespace.


# Did it work?
foreach(@temp)
{
    print  "($_)\n";  # use () to see if we have any white spaces.
}