in reply to hash unorderly output

You don't say what order you want the data back in. If you want alphabetical order by key, that's easy:

foreach my $grp (sort keys %grp_advise) { ...

If you want them back in the order you added them, that's not possible with a perl hash. Or at least, it's substantially more difficult. ;-)


s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&

Replies are listed 'Best First'.
Re^2: hash unorderly output
by Nevtlathiel (Friar) on May 25, 2005 at 10:23 UTC
    Not conceptually that much more difficult, you just have to maintain an array and push the key to it every time you add a key to your hash, then to get the output in the order it went in something like:

    foreach my $key (@ordered) { ...

    ----------
    My cow-orkers were talking in punctuation the other day. What disturbed me most was that I understood it.

      Nevtlathiel,
      you just have to maintain an array and push the key to it every time you add a key

      It is actually a bit more complicated than that though none of the problems can't be overcome. If you delete a key you either need to splice out the corresponding array element or do an exists test when looping over the array. If you change a key's value should it then go to the end or stay put? I am not saying that it is isn't that simple in specific cases, but in general it is usually better to just use an already made wheel:

      Cheers - L~R