在 perl 中获取数组中的多个值 [英] Getting many values in an array in perl
问题描述
我正在编写一个 prel 程序,其中我有一个包含以下模式的输入文件:
I am writing a prel program in which I have an input file containing a pattern as:
FIELDS=(1,2,3,4)
或
FIELDS=(1,10,3,A,11,10,7,D,9,10,11,A)
值的数量不是恒定的,而是 4 个.可能有 4、8、12、16 个或更多的值.
Number of values are not constant but in the bunch of 4. There could be 4,8,12, or 16 or more values.
我想将这些字段值保存在单独的变量中.我将其设置为
I want to save these field values in seperate variables. For which I am setting values as
if($filed1=~m/^\"SORT FIELDS"\s*=\s*"("\s*(.*?)[,]+(.*?)[,]+(.*?)[,]+(.*?)[,]+[,]*")"/sgim)
.
.
$val1 = $1;
$val2 = $2;
$val3 = $3;
$val4 = $4;
但这不会达到我的目的,因为每次都会有不同数量的值(4 或 8 或 12 ......).
But this will not serve my purpose as each time there will be different number of values (4 or 8 or 12..).
我看到的解决方案是将其保存在数组中,但我不知道如何将这些值保存在数组中.请告诉我是否可以做到.告诉我是否有其他方法可以得到结果.
The solution for this I see is to save it in an array, but I do not know how to save these values in array. Please tell me if I can do it. Tell me if there is any other method to get the result.
推荐答案
TIMTOWTDI.其中之一:
TIMTOWTDI. There is one of them:
use strict;
use warnings;
while (<DATA>) {
next unless /^FIELDS=\(([^)]*)\)/;
my @fields = split ',', $1;
while (@fields) {
my @subfields = splice @fields, 0, 4;
print "$. @subfields\n";
}
}
__DATA__
FIELDS=(1,2,3,4)
FIELDS=(1,10,3,A,11,10,7,D,9,10,11,A)
这篇关于在 perl 中获取数组中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!