开发者

Prohibit perl hash from sorting on creation

I created a hash in perl with this command:

%colum开发者_运维技巧ns = ("ID" , 10, "Time" , 25, "Cluster" , 25, "Type" , 10, "Hostname" , 30, "Service" , 25, "Comment" , 50);

I do not want the sort order modified in any way. However, Perl has taken it upon itself to sort them for me.

$VAR1 = 'ID';
$VAR2 = 10;
$VAR3 = 'Hostname';
$VAR4 = 30;
$VAR5 = 'Type';
$VAR6 = 10;
$VAR7 = 'Time';
$VAR8 = 25;
$VAR9 = 'Comment';
$VAR10 = 50;
$VAR11 = 'Cluster';
$VAR12 = 25;
$VAR13 = 'Service';
$VAR14 = 25;

How can I avoid this behavior?

Resolved:

 tie(%{$monstatus_settings{"comments"}->{"columns"}}, "Tie::IxHash", "ID" => 10, "Time" => 25, "Cluster" => 25, "Type" => 10, "Hostname" => 30, "Service" => 25, "Comment" => 50);


You can't. Hashes are explicitly unordered (actually, ordered by the computed hash for the key; that's why they're hashes). If you want to preserve order, use lists. Or use both, with the list being an ordered list of hash keys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜