我有一个CSV文件,我使用split
来解析N
项目的数组,其中N
是3
的倍数。在Perl中,我如何迭代数组的多个元素?
有没有一种方法,我可以做到这一点
foreach my ($a, $b, $c) (@d) {}
类似的Python?
我有一个CSV文件,我使用split
来解析N
项目的数组,其中N
是3
的倍数。在Perl中,我如何迭代数组的多个元素?
有没有一种方法,我可以做到这一点
foreach my ($a, $b, $c) (@d) {}
类似的Python?
您可以使用List::MoreUtils::natatime从文档:
my @x = ('a' .. 'g');
my $it = natatime 3, @x;
while (my @vals = $it->()) {
print "@vals\n";
}
natatime
在XS实现的,所以你应该更喜欢它的效率仅用于演示,这里是一个如何可能实现三。在Perl元素迭代器发电机:
#!/usr/bin/perl
use strict; use warnings;
my @v = ('a' .. 'z');
my $it = make_3it(\@v);
while (my @tuple = $it->()) {
print "@tuple\n";
}
sub make_3it {
my ($arr) = @_;
{
my $lower = 0;
return sub {
return unless $lower < @$arr;
my $upper = $lower + 2;
@$arr > $upper or $upper = $#$arr;
my @ret = @$arr[$lower .. $upper];
$lower = $upper + 1;
return @ret;
}
}
}
@z=(1,2,3,4,5,6,7,8,9,0);
for(@tuple=splice(@z,0,3); @tuple; @tuple=splice(@z,0,3))
{
print "$tuple[0] $tuple[1] $tuple[2]\n";
}
生产:
1 2 3
4 5 6
7 8 9
0
这破坏了@ z'数组,并可能更好地写成一个while循环 – 2010-07-08 22:41:29
@eric:true。这是一个快速解决方案。 – eruciform 2010-07-08 22:48:20
不容易。你会推迟作出@d
三元素的元组的阵列,通过推动元素到数组的数组引用更好:
foreach my $line (<>)
push @d, [ split /,/, $line ];
(除非你真的应该使用来自CPAN的CSV模块之一。
thx,这是一个快速的内部黑客,没想到它会如此艰难 – Timmy 2010-07-08 22:17:46
我我的CPAN模块List::Gen在解决了这个问题。
use List::Gen qw/by/;
for my $items (by 3 => @list) {
# do something with @$items which will contain 3 element slices of @list
# unlike natatime or other common solutions, the elements in @$items are
# aliased to @list, just like in a normal foreach loop
}
你也可以导入mapn
功能,用于通过List::Gen
实现by
:
use List::Gen qw/mapn/;
mapn {
# do something with the slices in @_
} 3 => @list;
他们实际上在别人的“为我”?或者只是在“for”循环中? “我的”应该做一个副本。 “通过”解决这个问题? – eruciform 2010-07-08 22:37:28
Perl foreach循环中的'my'变量绝不是副本,它始终是别名。一个词法范围的别名,但别名更少。 – 2010-07-08 22:39:08
我只能说*非常好!* – 2010-07-08 22:48:12
my @list = (qw(one two three four five six seven eight nine));
while (my ($m, $n, $o) = splice (@list,0,3)) {
print "$m $n $o\n";
}
此输出:
one two three
four five six
seven eight nine
不要使用'$一个'和'$ b'作为变量名称。它们是与'sort'一起使用的专门打包的范围变量。 – 2010-07-08 22:22:48
虽然,如果你可以这样做,那将会很酷。 – 2010-07-08 22:28:06
如果你在外面,那很好。但是如果它是一行一行的话,你可能会稍后重复使用,那么请小心,确实如此。 :-) – eruciform 2010-07-08 22:39:34