Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Syntax for casting map to a hash or an array

by 1nickt (Abbot)
on Apr 05, 2017 at 15:32 UTC ( #1187141=note: print w/replies, xml ) Need Help??

in reply to Syntax for casting map to a hash or an array

The outer pair of curlies with the sigil dereferences the anonymous hash created by the inner pair, acting on the list returned by the block in the innermost pair, which is the argument to map. keys wants a hash.

Also, Edit: Also, you likened "array" to:

$VAR1 = [ 'one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish' ];
This is a scalar holding a reference to an anonymous array. An array is differentiated from a list by context, not by square versus round brackets.

But for your last question, if you want an array containing the flattened hash derived from your map and then want to dumper it to get the output you showed:

say Dumper( [ map { $_ => "fish" } qw(one two red blue) ] );
Because square brackets return a reference to what map returns, so Data::Dumper prints as one data structure.

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Syntax for casting map to a hash or an array
by cbeckley (Curate) on Apr 05, 2017 at 15:40 UTC

    Aaaaah. I get it.

    So this is how to get the array:

    say Dumper(\@{ [map { $_ => 'fish' } qw(one two red blue)] } );

    I had tried

    say Dumper(\@[ {map { $_ => 'fish' } qw(one two red blue)} ] );
    after having half convinced my self the inner braces were a code block, but of course that didn't work either ...

    Thank you very much.


      Although it is technically a block, it's really an operator. %{} and @{} are often referred to as the "circumfix operators". They are available wholly to dereference what is within the block to whatever sigil is placed before it.

        Ah, more light bulbs. Thank you.

        And in my trivial example, these work:

        say Dumper({map { $_ => 'fish' } qw(one two red blue)}); say Dumper([map { $_ => 'fish' } qw(one two red blue)]);
        but I'm not sure I would have had either light bulb go off if I'd started there. Learning is strange trip. Oh, wait, maybe that's just me.


      \@{ [map { $_ => 'fish' } qw(one two red blue)] }

      Well, that works but it's a bit wasteful: first you're creating a arrayref with [], then you're dereferencing it with @{}, and then taking a reference to that again with \. It's easier to just use one set of []'s like 1nickt showed (although you may not have seen that yet since it was part of a large ninja edit).

        I got there! In the end. Somewhat circuitously ...

        I knew this

        %{ {map { $_ => 'fish' } qw(one two red blue)} }
        returned a hash ...

        And I knew Dumper needed a reference ...

        And I suspected that if I could successfully pass Dumper a reference to an array I might finally understand what was going on with

        %{ { ... } }
        that bit.

        Thank you very much.


      Nope, one level too many of referencing / deferencing. See my example above.

      With this kind of thing it's best to strip it down to the innermost element and print its output with a data dumper. Then when you understand what is going on, add a layer, repeat. Trying to get at it from the outer edge when you are confused is a bit like flailing. A methodical approach is most effective.

      The way forward always starts with a minimal test.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2020-12-02 17:24 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (44 votes). Check out past polls.