【发布时间】:2014-11-07 13:39:33
【问题描述】:
我已经进行了一些搜索,并努力找到了这个问题的答案。
假设我有一个这样的哈希数组..
my @AoH = (
{ 'ip_type' => 1,
'ip_ref' => 0001,
'ip_address' => '192.168.0.1',
'ip_priority' => '100'
},
{ 'ip_type' => 1,
'ip_ref' => 0002,
'ip_address' => '192.168.0.2',
'ip_priority' => '1'
}
);
在上下文中,这些是多个 IP 地址,我打算在其中进行负载平衡。 'ip_priority' 值决定了哪个是第一个,然后是第二个。主要和备份,基本上。
但是,我想根据散列中的 ip 优先级值对数组元素(也称为散列)进行数字排序。
所以,上面的 AoH 最好是……
my @AoH = (
{ 'ip_type' => 1,
'ip_ref' => 0002,
'ip_address' => '192.168.0.2',
'ip_priority' => '1'
},
{ 'ip_type' => 1,
'ip_ref' => 0001,
'ip_address' => '192.168.0.1',
'ip_priority' => '100'
}
);
所以$AoH[0]->{ip_priority} 现在包含 1,$AoH[1]->{ip_priority} 包含 100。
如何执行排序以实现上面的第二个示例?我正在努力寻找一个可行的例子!
【问题讨论】:
标签: arrays perl data-structures hash