Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Specific hash to array conversion query

by davidrw (Prior)
on Jul 12, 2005 at 02:45 UTC ( #474154=note: print w/replies, xml ) Need Help??

in reply to Specific hash to array conversion query

Here's a map solution (note that you can't depend on the order that keys provides, so if order matters you'll have to put a sort or two in there; I also assumed that there was just 0 and 1 keys in there):
my @values = map { ( [ $_, 0, $properties{$_}->{0}->{value} ], [ $_, 1, $properties{$ +_}->{1}->{value} ] ) } keys %properties;
Also note that your %properties = { ... } needs to be %properties = ( ... )

Replies are listed 'Best First'.
Re^2: Specific hash to array conversion query
by monarch (Priest) on Jul 12, 2005 at 02:51 UTC
    Nice answer, thankyou!

    What if there were other keys besides 0 and 1.. that appears to be the hardest part of all..

      thanks! For other keys, you can nest a map in there .. trick is to use a temp variable for the outer $_. Remember that you can put any code inside the map--it's only the value of the last statement that matters/is returend. I also tossed a sort of the file results in there so that it's exactly what you put in your OP:
      my @values = sort { $b->[0] cmp $a->[0] || $a->[1] <=> $b->[1] } map { my $k = $_; map { [ $k, $_, $properties{$k}->{$_}->{value} ] } keys %{$propert +ies{$k}}; } keys %properties;
      Update: Now that i read the rest of the thread and see that Zaxo and Transient provided the exact same nested map.. I like the post-sort on mine though :)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2020-05-25 05:15 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (143 votes). Check out past polls.