加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

数组 – Perl推入排序数组

发布时间:2020-12-15 21:55:14 所属栏目:大数据 来源:网络整理
导读:考虑下面的数据块,我如何维护第3个字段对数组进行排序,并继续推送项目? $VAR1 = [ '1111','http://...',3 #this is one of the 3rd field mentioned above ];$VARN = [ '5555',0 ]; 我的代码看起来像: my @curItem = ($item-{id},$item-{href},getTotal( $
考虑下面的数据块,我如何维护第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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读