Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re: Specific hash to array conversion query by revdiablo
in thread Specific hash to array conversion query by monarch

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (7)
    As of 2020-07-02 12:13 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found