考虑下面的数据块,我如何维护第3个字段对数组进行排序,并继续推送项目?
$VAR1 = [
'1111','http://...',3 #this is one of the 3rd field mentioned above
];
$VARN = [
'5555',0
];
我的代码看起来像:
my @curItem = ($item->{id},$item->{href},getTotal( $item->{id}) );
push @items,\@curItem;
我找到了this模块,它类似于我需要的模块.
任何帮助赞赏.
解决方法
您可以使用该模块,只需提供排序:
tie @a,“Tie :: Array :: Sorted”,sub {$_ [0] – > [2]< => $_ [1] – > [2]};
(或者沿着这些方向的东西……我将不得不检查它.基本上,你需要根据你传入的数组ref的元素进行排序)
编辑:是的,这适用于您的数据.刚检查一下:
use Tie::Array::Sorted;
tie @a,"Tie::Array::Sorted",sub { $_[0]->[2] <=> $_[1]->[2] };
push @a,[ "1111","http:// ...",3];
push @a,[ "5555",0];
foreach $ref (@a)
{
print $ref . "\n";
print "@$ref \n";
}
输出:
ARRAY(0x9130888) 5555 http:// ... 0 ARRAY(0x90dd818) 1111 http:// ... 3