您的位置:首页 > 其它

Perl的那些技巧:平均切割数组

2013-11-30 11:42 375 查看
有些时候需要将数组分成长度相同的多个子数组,比如批量查询数据库。

这时,我们可以用splice完成这个功能。代码如下:

#!/usr/bin/env perl
use strict;
use warnings;
my @chunks;
my $size = 10;
my @array = (1..100);
push @chunks, [splice(@array, 0, $size)] while @array;
foreach my $ref (@chunks) {
print join("\t", @$ref)."\n";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: