Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^3: sorting a complex multidimensional hash

by envirodug (Novice)
on Jul 22, 2004 at 19:29 UTC ( #376693=note: print w/ replies, xml ) Need Help??

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

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 ./ 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

Comment on Re^3: sorting a complex multidimensional hash
Select or Download Code
Replies are listed 'Best First'.
Re^4: sorting a complex multidimensional hash
by BrowserUk (Pope) on Jul 22, 2004 at 20:47 UTC

    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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://376693]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2016-02-10 20:08 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (354 votes), past polls