我正在尝试从 Perl 中的输入文件中读取值。输入文件如下所示:
1-sampledata1 This is a sample test
and data for this continues
2-sampledata2 This is sample test 2
Data for this also is on second line
我想读取上面的数据,以便数据1-sampledata1
进入@array1
和数据2-sampledata2
进入@array2
等等。我将有大约 50 个这样的部分。喜欢50-sampledata50
。
编辑:名称不会总是 X-sampledataX。例如,我只是这样做了。所以名字不能循环。我想我必须手动输入它们
到目前为止,我有以下(有效)。但我正在寻找一种更有效的方法来做到这一点..
foreach my $line(@body){
if ($line=~ /^1-sampledata1\s/){
$line=~ s/1-ENST0000//g;
$line=~ s/\s+//g;
push (@array1, $line);
#using splitarray because i want to store data as one character each
#for ex: i wana store 'This' as T H I S in different elements of array
@splitarray1= split ('',$line);
last if ($line=~ /2-sampledata2/);
}
}
foreach my $line(@body){
if ($line=~ /^2-sampledata2\s/){
$line=~ s/2-ENSBTAP0//g;
$line=~ s/\s+//g;
@splitarray2= split ('',$line);
last if ($line=~ /3-sampledata3/);
}
}
如您所见,每个部分都有不同的数组,每个部分都有不同的 for 循环。如果我采用到目前为止的方法,那么我最终将得到 50 个 for 循环和 50 个数组。
还有另一种更好的方法吗?最后,我确实想得到 50 个数组,但不想写 50 个 for 循环。由于稍后我将在程序中循环遍历 50 个数组,也许将它们存储在一个数组中?我是 Perl 的新手,所以它有点压倒性......