Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

One of the answers to Why does ‘keys’ need a named hash? made me remember a question that's been bugging me for awhile.

duelafn replied with

return keys %{ {map { $_ => 1 } @_} };

I've been using this syntax and have accepted it as idiomatic Perl, however, what I don't get is, why the curly braces? And why two pairs of them? Is there a reference dereference happening?

Given this:

say Dumper(\%{ {map { $_ => 'fish' } qw(one two red blue)} } ); ^ ^ ^ ^ |-|-----------------------------------------|-|
I get:
$VAR1 = { 'blue' => 'fish', 'one' => 'fish', 'red' => 'fish', 'two' => 'fish' };
If I remove the inner pair of indicated braces, I get
Ambiguous use of %{map{...}} resolved to %map{...}

So the inner pair is a code block. I'm not sure that clears anything up for me, unless I'm being thick ...
If I need to return an array, as opposed to a list or a hash, what would that syntax be?
So if I want this:

$VAR1 = [ 'one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish' ];
How do I do it?

say Dumper(\@{ {map { $_ => 'fish' } qw(one two red blue)} } );
Not an ARRAY reference
Which implies this is not a dereferencing thing ... unless of course I'm doing it wrong.


In reply to Syntax for casting map to a hash or an array by cbeckley

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 making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2020-11-27 15:15 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found