Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Specific hash to array conversion query

by revdiablo (Prior)
on Jul 12, 2005 at 16:44 UTC ( [id://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 => '132.181.30.3', fixed => 0 }, 1 => { value => '132.181.30.4', 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-19 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found