Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Hash key ordering question

by P0w3rK!d (Pilgrim)
on May 07, 2003 at 18:08 UTC ( #256334=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I built a HoH as follows:
# build hash while() { ... $hshFoo{$key1}{$key2} = $strFileName; ... # key1 is a number # key2 is a sequential number }
The entries are placed in seqential order such as:
$hshFoo{2823}{0} = 2823_0.xml $hshFoo{2823}{1} = 2823_1.xml $hshFoo{2823}{2} = 2823_2.xml $hshFoo{2823}{3} = 2823_3.xml
When I dump the hash, the keys come out as follows:
foreach $key1 (keys %hshFoo) { foreach $key2 (keys %{$hshFoo{$key1}}) { print "$key1/$key2 => $hshFoo{$key1}{$key2}\n"; } } ... Output: 2823/1 => 2823_1.xml 2823/0 => 2823_0.xml 2823/3 => 2823_3.xml 2823/2 => 2823_2.xml ... I expected: 2823/0 => 2823_0.xml 2823/1 => 2823_1.xml 2823/2 => 2823_2.xml 2823/3 => 2823_3.xml
The files were originally placed in the hash from a sorted array. Are the keys not in order within the hash or does Perl just grab the keys in any order?
Just curious. :)


Replies are listed 'Best First'.
Re: Hash key ordering question
by thelenm (Vicar) on May 07, 2003 at 18:13 UTC
    Yep, hash entries are not ordered in a predictable way. You will have to sort the hash keys yourself, or you may want to look into a module like Tie::IxHash, which preserves the insertion order of hash keys.

    -- Mike


      Thank you :)
Re: Hash key ordering question
by artist (Parson) on May 07, 2003 at 18:20 UTC
Re: Hash key ordering question
by jkenneth (Pilgrim) on May 07, 2003 at 18:52 UTC
    Ummm, the numbers are sequential, so why not use an array?
    for ($i = 0; $i < 10; $i++) { $hash{2823}[$i] = "2823_$i.xml"; } foreach $key (keys %hash) { foreach $xml (0..$#{$hash{$key}}) { print "Key: $key/$xml => " . $hash{$key}[$xml] . "\n"; } } Key: 2823/0 => 2823_0.xml Key: 2823/1 => 2823_1.xml Key: 2823/2 => 2823_2.xml Key: 2823/3 => 2823_3.xml Key: 2823/4 => 2823_4.xml Key: 2823/5 => 2823_5.xml Key: 2823/6 => 2823_6.xml Key: 2823/7 => 2823_7.xml Key: 2823/8 => 2823_8.xml Key: 2823/9 => 2823_9.xml

      I would guess the datastructure is part of a larger program where an array doesn't make sense.

      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

Re: Hash key ordering question
by jgallagher (Pilgrim) on May 07, 2003 at 18:30 UTC
    If you want them in numerical order rather than the order you put them in, you could also do:
    foreach $key1 (sort keys %hshFoo) { foreach $key2 (sort keys %{$hshFoo{$key1}}) { print "$key1/$key2 => $hshFoo{$key1}{$key2}\n"; } }
      that will actually sort in string comparison order (the default for sort). for numeric order, you want:
      ... foreach $key1 (sort {$a <=> $b} keys %hshFoo) { ...
        This is what I was looking for. I was trying to do something with Tie::IxHash the last 10 minutes.
        my $t = Tie::IxHash->new(%hshFoo); my @keys = $t->Keys; $t->Reorder(@keys); $t->SortByKey; untie %hshFoo; ...or something


        Thanks. That completely slipped my mind.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://256334]
Approved by jonnyfolk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-09-21 13:34 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (126 votes). Check out past polls.