Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用另一个数组对散列perl进行排序_Perl_Sorting - Fatal编程技术网

使用另一个数组对散列perl进行排序

使用另一个数组对散列perl进行排序,perl,sorting,Perl,Sorting,我正在尝试使用列表/数组对哈希进行排序 my $hash = { cta => '01340031810312074443', ttr => '001',fil => '0000', ref => '0000', mef => '0000000000000060000', mch => '00000

我正在尝试使用列表/数组对哈希进行排序

my $hash =  {     cta => '01340031810312074443',
                  ttr => '001',fil => '0000',     
                  ref => '0000',
                  mef => '0000000000000060000',
                  mch => '0000000000000000000',
                  nli => '00000000',
                  tdi => 'V',
                  ndi => '006126952',
                  tdip => 'V',
                  ndip => '006126952',
              };

@order = qw(cta ttr fil ref mef mch nli tdi ndi tdip ndip);
我们知道Perl不会以散列形式保存订单,但我需要按该顺序打印。我该怎么做

谢谢

试试这个:

for (@order) {
    print $_, " => ", $hash->{$_}, "\n";
}

如果只想打印值而不打印键,还可以使用哈希片:

use feature 'say';
say join "\t", @hash{@order};

如何按数组
@order
中存储的顺序打印密钥?你真的需要考虑一下吗?
print$hash->{$}for@order
噢,不,你没有使用
来连接变量和字符串
print“$key:$hash->{$key}\n”
我自己也养成了这个习惯,因为你必须跳转来插值
说“$object的结果是$object->Result”
use feature 'say';
say join "\t", @hash{@order};