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)} } );
produces
Not an ARRAY reference
Which implies this is not a dereferencing thing ... unless of course I'm doing it wrong.
Thanks,
cbeckley
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.