Eug*_*kov 0 perl data-structures
我有下一个哈希:
%hash = (
name => {
pos => 1
},
name_xxx => {
pos => 2
},
name_yyy => {
pos => 3
},
)
Run Code Online (Sandbox Code Playgroud)
并且想要构造下一个数组(键必须按顺序排列pos):
qw/ name name_xxx name_yyy /
Run Code Online (Sandbox Code Playgroud)
我想我应该做Schwartzian变换
以给定顺序提取哈希键的最短和/或最快方法是什么?
你拥有的不是哈希,它是一个哈希引用(缺少逗号).要获取密钥,请使用密钥和取消引用:
#!/usr/bin/perl
use warnings;
use strict;
my $hash_ref = {
name => {pos => 1},
name_xxx => {pos => 2},
name_yyy => {pos => 3},
};
my @keys = sort { $hash_ref->{$a}{pos} <=> $hash_ref->{$b}{pos} }
keys %$hash_ref;
print "@keys\n";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |