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

Re: Specific hash to array conversion query

by revdiablo (Prior)
on Jul 12, 2005 at 16:44 UTC ( #474334=note: print w/replies, xml ) Need Help??

in reply to Specific hash to array conversion query

I could loop over the hash and build up the array, but I would like to be more clever and use the map { } function.

I'd like to step back for a moment. This node is interesting, and the map solutions are very neat and clever. I know we all like writing this kind of code. It's certainly exciting when you can look at a block of code like this and understand what it's doing.

I have to wonder if it's the best approach, though. An iterative solution would be easier to write, and easier to read later, saving time on both ends. I guess it's a moot point now that the clever solutions have been provided, but it might be worth thinking about.

Incidentally, if the thing you don't like about the iterative solution is that you have to use push instead of having everything as an rvalue (which I admit, does feel kind of clumsy), then I have good news for you. This is yet another thing Fixed in 6:

%properties = ( if_mac => { 0 => { value => '00:11:1A:F2:E1:92', fixed => 0 }, 1 => { value => '00:11:1A:F2:E1:93', fixed => 0 }, }, if_ip => { 0 => { value => '', fixed => 0 }, 1 => { value => '', fixed => 0 }, } ); my @properties = gather { for %properties.keys -> $type { for %properties{$type}.keys -> $num { take [ $type, $num, %properties{$type}{$num}{'value'} ]; } } };

The gather/take construct allows you to use an iterative algorithm as an rvalue. I think I will end up using it a lot. :-)

Update: some may point out that map really is iterative, just in a sort of interesting way. I guess that's true. So perhaps s/iterative/for loop/g on my node.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2020-06-07 05:41 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (42 votes). Check out past polls.