当我打印Perl哈希时,什么决定了键的顺序? [英] What decides the order of keys when I print a Perl hash?

查看:208
本文介绍了当我打印Perl哈希时,什么决定了键的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于activePerl 5.8

 #!C:\Perl\bin\perl.exe 
use严格;
使用警告;

#声明一个新的散列
my%some_hash;

some_hash =(foo,35,bar,12.4,2.5,hello,
wilma,1.72e30,betty,bye\\\
);

my @any_array;
@any_array =%some_hash;

print%some_hash;
打印\\\
;
print @any_array;
打印\\\
;
print $ any_array [0];
打印\\\
;
print $ any_array [1];
打印\\\
;
print $ any_array [2];
打印\\\
;
print $ any_array [3];
打印\\\
;
print $ any_array [4];
打印\\\
;
print $ any_array [5];
打印\\\
;
print $ any_array [6];
打印\\\
;
print $ any_array [7];
打印\\\
;
print $ any_array [8];
打印\\\
;
print $ any_array [9];

输出为此

  D:\learning\perl> test.pl 
bettybye
bar12.4wilma1.72e + 030foo352.5hello
bettybye
bar12.4wilma1.72e + 030foo352.5hello
betty
bye

bar
12.4
wilma
1.72e + 030
foo
35
2.5
hello
D:\learning\perl>

什么决定了我的示例代码中的元素打印顺序?

在Perl中打印混合(字符串,数字)散列时要遵循的规则是什么?谢谢。

  bar12.4wilma1.72e + 030foo352.5hello 

[已更新]



 #!C:\Perl\bin\perl.exe 
use strict;
使用警告;

#声明一个新的散列
my%some_hash;

some_hash =(foo,35,bar,12.4,2.5,hello,
wilma,1.72e30,betty,bye);

my @any_array;
@any_array =%some_hash;

print%some_hash;
打印\\\
;
打印\\\
;
print @any_array;
打印\\\
;
打印\\\
;

我的@keys;
@keys =键%some_hash;
for my $ k(sort @keys)
{
print $ k,$ some_hash {$ k};
}

输出

  D:\learning\perl> test.pl 
bettybyebar12.4wilma1.72e + 030foo352.5hello

bettybyebar12.4wilma1.72e + 030foo352。 5hello

2.5hellobar12.4bettybyefoo35wilma1.72e + 030
D:\learning\perl>

最后,在调用和<$之后c $ c> sort 函数。哈希键打印遵循以下规则:

  2.5hellobar12.4bettybyefoo35wilma1.72e + 030 


解决方案

散列元素以内部顺序打印出来,不能依赖并且会改变因为元素被添加和删除。如果您需要按某种顺序排列哈希的所有元素,请对键进行排序,然后使用该列表对哈希进行索引。



如果您正在寻找一个按顺序保存其元素的结构,请使用数组,或者使用CPAN上的某个有序哈希。



您可以从列表上下文哈希扩展中唯一依赖的顺序是:key =>值对将会在一起。


activePerl 5.8 based

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

# declare a new hash
my %some_hash;

%some_hash = ("foo", 35, "bar", 12.4, 2.5, "hello",
      "wilma", 1.72e30, "betty", "bye\n");

my @any_array;
@any_array = %some_hash;

print %some_hash;
print "\n";
print @any_array;
print "\n";
print $any_array[0];
print "\n";
print $any_array[1];
print "\n";
print $any_array[2];
print "\n";
print $any_array[3];
print "\n";
print $any_array[4];
print "\n";
print $any_array[5];
print "\n";
print $any_array[6];
print "\n";
print $any_array[7];
print "\n";
print $any_array[8];
print "\n";
print $any_array[9];

Output as this

D:\learning\perl>test.pl
bettybye
bar12.4wilma1.72e+030foo352.5hello
bettybye
bar12.4wilma1.72e+030foo352.5hello
betty
bye

bar
12.4
wilma
1.72e+030
foo
35
2.5
hello
D:\learning\perl>

What decided the elements print order in my sample code?

Any rule to follow when print a mixed(strings, numbers) hash in Perl? Thank you.

bar12.4wilma1.72e+030foo352.5hello

[Updated]

With you guys help, i updated the code as below.

#!C:\Perl\bin\perl.exe
use strict;
use warnings;

# declare a new hash
my %some_hash;

%some_hash = ("foo", 35, "bar", 12.4, 2.5, "hello",
      "wilma", 1.72e30, "betty", "bye");

my @any_array;
@any_array = %some_hash;

print %some_hash;
print "\n";
print "\n";
print @any_array;
print "\n";
print "\n";

my @keys;
@keys = keys %some_hash;
for my $k (sort @keys)
{
    print $k, $some_hash{$k};
}

output

D:\learning\perl>test.pl
bettybyebar12.4wilma1.72e+030foo352.5hello

bettybyebar12.4wilma1.72e+030foo352.5hello

2.5hellobar12.4bettybyefoo35wilma1.72e+030
D:\learning\perl>

Finially, after called keys and sort functions. The hash keys print followed the rule below

2.5hellobar12.4bettybyefoo35wilma1.72e+030

解决方案

Elements of a hash are printed out in their internal order, which can not be relied upon and will change as elements are added and removed. If you need all of the elements of a hash in some sort of order, sort the keys, and use that list to index the hash.

If you are looking for a structure that holds its elements in order, either use an array, or use one of the ordered hash's on CPAN.

the only ordering you can rely upon from a list context hash expansion is that key => value pairs will be together.

这篇关于当我打印Perl哈希时,什么决定了键的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆