【问题标题】:How can I do natural sort in hash?如何在哈希中进行自然排序?
【发布时间】: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


【解决方案1】:

Perl 散列的元素在设计上是随机排列的。您无法对哈希执行任何操作来设置其内容的顺序。

可以做的是以特定顺序处理或输出散列的元素。比如

for my $k ( sort keys %hash_keysort ) {
    printf "%-12s : %s\n", $k, $hash_keysort{$k};
}

如果您需要比这更多的帮助,那么您必须改进您的问题。特别是您需要显示您使用的代码。 “我尝试使用此软件包使用 Sort::Naturally 信息不足。

如果您希望输出按特定顺序而不是排序,那么唯一的方法是单独定义该顺序。

my @keys = qw/ servlet servlet_dude hai array /;

for my $k ( @keys ) {
    my $v = $hash_keysort{$k};
    $v = $v->[0] if ref $v;
    printf "%-12s : %s\n", $k, $v;
}

【讨论】:

  • 我的问题是如果你对哈希进行排序。密钥不应每次都更改。
  • @DushyanthSimha 您无法对哈希进行排序。您可以获取散列的键,以特定方式对它们进行排序,然后为每个键输出键和值。但这不会改变哈希。每个设计的哈希不像电话簿那样对条目进行排序,而是像一袋拼字游戏,但您可以确切地知道每个字母在袋子中的位置。
  • #!/usr/bin/perl 使用 Sort::Naturally ;使用严格;使用警告;我的价值;我的 $key;我的 %hash_keysort = (servlet => 'true',servlet_dude => 123,hai =>"null", array => [123,"null","string"]);对于我的 $k ( 排序键 %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
  • 我每次都需要以相同的顺序获取钥匙。但关键是药水一直在变化。是否有任何逻辑可以以相同的顺序获取钥匙。我的意图是我需要得到钥匙而不是每次都更换他们的药水
  • @DushyanthSimha 我不明白你在说什么。如果我多次运行您的代码,每次都会产生相同的结果。唯一改变的是字符串化数组引用地址。
【解决方案2】:

从更新来看,它不再是排序问题。您正在寻找的订单不适合任何排序程序。相反,如果有一组离散的键并且您希望按该顺序输出,则可以创建一个数组用于迭代:

#!/usr/bin/perl
use strict;
use warnings;

my $value;
my $key;
my %hash_keysort = (servlet => 'true',servlet_dude => 123,hai =>"null", array => [123,"null","string"]);

my @wanted = qw(servlet servlet_dude hai array);
for my $k ( @wanted ) {
    printf "%-12s : %s\n", $k, $hash_keysort{$k};
}

这将允许您始终按您想要的顺序获得输出。

【讨论】:

  • 感谢您的回复。但这对我没有帮助。现在我更新了新代码。你能帮帮我吗。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 2011-02-17
  • 2023-03-14
  • 2011-03-10
相关资源
最近更新 更多