【发布时间】:2017-09-05 11:11:34
【问题描述】:
use strict;
use warnings;
use 5.010;
my $value;
my $key;
my %hash_keysort = (
servlet => 'true',
servlet_dude => 123,
hai => "null",
array => 123
);
我想得到相同的输出。
我试过这个包使用Sort::Naturally。
即使我使用自然排序,我也无法获得相同的输出。 每次密钥都在变化。
预期输出:
servlet : true
servlet_dude : 123
hai : null
array : 123
我也尝试了nsort 功能。我拿不到钥匙。
我尝试了所有排序类型。我无法解决问题。
我每次都需要以相同的顺序获取钥匙。但关键是药水一直在变化。是否有任何逻辑可以以相同的顺序获取钥匙。 我的意图是我需要得到钥匙而不是每次都换药水。
更新
#!/usr/bin/perl
use Sort::Naturally;
use strict;
use warnings;
my $value;
my $key;
my %hash_keysort = (
servlet => 'true',
servlet_dude => 123,
hai => "null",
array => [ 123, "null", "string" ]
);
for my $k ( sort keys %hash_keysort ) {
printf "%-12s : %s\n", $k, $hash_keysort{$k};
}
实际输出
array : ARRAY(0x8002afb8)
hai : null
servlet : true
servlet_dude : 123
预期
servlet : true
servlet_dude : 123
hai : null
array : 123
更新了一个: #!/usr/bin/perl 使用严格; 使用警告;
#binmode STDOUT, ":utf8";
use utf8;
use Data::Dumper;
use JSON;
use Scalar::Util 'reftype';
my $json;
local $/; #Enable 'slurp' mode
open my $fh, "<", "my.json"; #opening a file
$json = <$fh>;
close $fh;
my $data = decode_json($json); #decodeing the json data
print "$data\n";
print Dumper($data); #prints HASH table
print "\n";
my $key;
my $value;
my $initialtag = ''; #declaring the intial tag of the
data
my $hash = {'HASH'=>9998,'ARRAY'=>9987,'SCALAR'=>9996}; # initializing the
Hash table for opcodes
my $first_key;
my $tag_len;
my $opcode;
my $hex;
my $finalkey_opcode;
my $firstvalue;
my $input;
my $initialtag_2;
my $firstvalue_2;
my $datavalue = check_refernces($data);
if($datavalue){
$datavalue = check_refernces($datavalue);
}
$datavalue = check_refernces($datavalue);
sub Hash_reference { 我的 $input=shift; foreach $key (keys %$input) {
$firstvalue = $input->{$key}; #Acessing the value(Getting
value from hash)
print " the value of key $key $value\n";
$initialtag = $key;
}
print "Variable is HASH\n\n";
$opcode=$hash->{'HASH'}; #If it is hash Getting opcode
for object
print "the opcode is $opcode\n";
my $tag_len = length( $initialtag ); #Finding the length
print "$tag_len\n";
my $hex =sprintf('%04X',$tag_len); #Converting to hexadecimal
format
print "$hex\n";
my $first_key = $hex.$initialtag; #concatenating the Intail tag
and hexadecimal value(length of the data)
print "$first_key\n";
my $finalkey_opcode=$opcode.$first_key; #concatenating the opcode and
data
print "$finalkey_opcode\n";
return $firstvalue;
}
sub Scalar_reference
{
print "Variable is SCALAR refernce\n"; # Not a reference
{
my $input=shift;
$opcode=$hash->{'SCALAR'}; #If it is hash Getting opcode
for object
print "the opcode is $opcode\n";
my $tag_len = length( $initialtag_2 ); #Finding the length
print "$tag_len\n";
my $hex =sprintf('%04X',$tag_len); #Converting to hexadecimal
format
print "$hex\n";
my $first_key = $hex.$initialtag_2; #concatenating the Intail
tag and hexadecimal value(length of the data)
print "$first_key\n";
my $finalkey_opcode=$opcode.$first_key; #concatenating the opcode
and data
print "$finalkey_opcode\n";
return $firstvalue_2;
}
}
sub Array_refernce
{
print "Variable is ARRAY\n\n"; # Reference to a ARRAY
}
sub code_refernce
{
print "Variable is CODE\n\n"; # Reference to a CODE
}
sub check_refernces
{
my $input = shift;
my $returnvalue;
if (ref($input)eq 'SCALAR')
{
Scalar_reference();
}
elsif (ref($input)eq 'HASH')
{
$returnvalue = Hash_reference($input);
# Reference to a hash
}
elsif (ref($input)eq 'ARRAY')
{
Array_refernce();
}
elsif (ref($value)eq 'CODE')
{
code_refernce();
}
}
数据文件是:
“网络应用”:{
“小服务程序”:
{
“servlet 名称”:是的,
“servlet 类”:123,
“海”:空,
"数组":[123,null,"字符串"]
}}
预期输出: 小服务程序:真 servlet_dude:123 海:空 数组:数组(0x8002afb8)
如何获得相同的输出。
【问题讨论】:
-
您好,我已经修复了您的代码格式。请以后自己做。
-
“获取输出”到底做了什么?您不能在哈希中排序键。
-
您是如何尝试对哈希进行排序的?显示您正在使用的代码以便我们了解可能出现的问题是必要的。
-
看到更新后,我认为这不是排序问题。您无法通过排序获得所需的顺序。也许相反,将键加载到数组中并对其进行迭代?
-
我真的不清楚你所说的“自然排序”是什么意思。您尝试使用的模块 Sort::Naturally 对字符串进行词汇排序和数字排序。但这根本不符合你的描述。对您而言,什么是“自然排序”?
标签: perl perl-module