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

前端之家收集整理的这篇文章主要介绍了Perl的那些技巧:平均切割数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有些时候需要将数组分成长度相同的多个子数组,比如批量查询数据库

这时,我们可以用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";
}

猜你在找的Perl相关文章