http://qs1969.pair.com?node_id=376552


in reply to Re: sorting a complex multidimensional hash
in thread sorting a complex multidimensional hash

Since there has been all the dicussion regarding ST -v- GRT, here's an multi-level GRT version. Runs about twice as fast.

print for map{ substr( $_, 1+index( $_, '|' ) ) } sort map { my $key1 = $_; map { pack 'NA* NA* A1 A*', split( '\.', $key1 ), split( '\.', $_ ), '|', $hash{ $key1 }{ $_ } } keys %{ $hash{ $_ } }; } keys %hash;

Just plug it into the test script above to see it run.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^3: sorting a complex multidimensional hash
by wfsp (Abbot) on Jul 22, 2004 at 16:58 UTC
    You may be giving perl a bad reputation! Terse? No, not at all!
    Seriously, I'm impressed but glad I don't have to maintain it!
    Twice as fast you say. Hmmm. Tempting...

      You should see the recursive version for processing arbitrarially deep nested hashes :)

      On a serious note. I find short to be good for mainatainence. It may require me to look longer before I start making changes, but I consider that a good thing.

      Too many times I've been bitten by zeroing in on what looked to be (or the comments indicated as), a self contained piece of code, only to find out later that there was another piece just off screen that was affected.

      It's a personal thing, but if I don't understand a piece of code froom reading it, then I prefer to experiment (in a test program or with the debugger) until I do, rather than be swayed into believing that I understand it by reading another programmer's, or even my own, comments.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re^3: sorting a complex multidimensional hash
by envirodug (Novice) on Jul 22, 2004 at 19:29 UTC
    i can definately handle faster ...

    question: the output from the first example was formatted as following:  "{$_->[ 0 ]}{$_->[ 1 ]} => $hash{ $_->[ 0 ] }{ $_->[ 1 ] }"

    the output from the new example prints the 'values' of the hash: substr( $_, 1+index( $_, '|' ) ).

    i tried substituting "{$_->[ 0 ]}{$_->[ 1 ]} => $hash{ $_->[ 0 ] }{ $_->[ 1 ] }" from the first example into the second example where substr( $_, 1+index( $_, '|' ) ) was found, but i'm seeing an error: Can't use string ("") as an ARRAY ref while "strict refs" in use at ./test4.pl line 42

    would anyone know how I would need to format this code ({$_->[ 0 ]}{$_->[ 1 ]} => $hash{ $_->[ 0 ] }{ $_->[ 1 ] }) to work in the second example and print information formatted similar to the first example? (i know enough perl to 'get myself in trouble')

    again, many thanks for everyone's help

      That's not too hard to do:

      print for map{ my( $n1, $a1, $n2, $a2, $value ) = unpack 'N A4 N A4 A*', $_; "{$n1.$a1}{$n2.$a2} => $value" } sort map { my $key1 = $_; map { pack 'N A4 N A4 A*', split( '\.', $key1 ), split( '\.', $_ ), $hash{ $key1 }{ $_ } } keys %{ $hash{ $_ } }; } keys %hash;

      but it does introduce a limitation.

      As coded above, the character part of the keys is limited to 4 characters each. They can be shorter but no longer. If this isn't long enough, you must adjust the formats ( 'N A4 N A4 A*' ) to say 'N A10 N A10 A*' for up to 10 chars, in both the pack and the unpack.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon