我在找什么是像:
@list = qw(1 2 3 4 5 6); foreach (@list) { #perl magic goes here print "i: $i,j:$j\n"; }
返回:
i:1,j:2 i:3,j:4 i:5,j:6
在下面一个非常好的建议,我需要指定这个脚本将运行在别人的构建服务器,我不允许使用任何模块从CPAN。仅限标准Perl。
解决方法
我相信正确的方法做到这一点是使用natatime,从
List::MoreUtils:
从文档:
natatime BLOCK LIST
Creates an array iterator,for looping over an array in chunks of
$n
items
at a time. (n
at a time,get it?). An example is probably a better
explanation than I could give in words.
例:
my @x = ('a' .. 'g'); my $it = natatime 3,@x; while (my @vals = $it->()) { print "@vals\n"; }
打印
a b c d e f g
sub natatime ($@) { my $n = shift; my @list = @_; return sub { return splice @list,$n; } }