【发布时间】:2012-04-30 10:54:59
【问题描述】:
可能重复:
Easiest way to open a text file and read it into an array with Perl
我是 Perl 的新手,希望每个文件都将文件的内容推送到一个单独的数组中,我通过以下方法设法做到了这一点,它使用 if 语句。但是,我想为我的阵列提供 1 美元之类的东西。那可能吗?
#!/usr/bin/perl
use strict;
my @karray;
my @sarray;
my @testarr = (@sarray,@karray);
my $stemplate = "foo.txt";
my $ktemplate = "bar.txt";
sub pushf2a {
open(IN, "<$_[0]") || die;
while (<IN>) {
if ($_[0] eq $stemplate) {
push (@sarray,$_);
} else {
push (@karray,$_);
}
}
close(IN) || die $!;
}
&pushf2a($stemplate,@sarray);
&pushf2a($ktemplate,@karray);
print sort @sarray;
print sort @karray;
我想要这样的东西:
#!/bin/sh
myfoo=(@s,@k)
barf() {
pushtoarray $1
}
barf @s
barf @k
【问题讨论】:
标签: perl