Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

by cbeckley (Curate)
on Apr 05, 2017 at 15:40 UTC ( #1187144=note: print w/replies, xml ) Need Help??

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

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.


Replies are listed 'Best First'.
Re^3: Syntax for casting map to a hash or an array
by stevieb (Canon) on Apr 05, 2017 at 15:48 UTC

    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.


        Yep, both of those work, however not for the same reason.

        Dumper takes a reference of any type.


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

        ... takes whatever is compiled by map, then with the outer braces, converts that into a hash reference, then returns the hash ref (same for the next one with an array). Putting the sigil in front of that block, and it will deref the created reference into a hash, and return it as a hash as opposed to a reference. For Dumper, you'd then need to dereference it before sending it in as a parameter.

Re^3: Syntax for casting map to a hash or an array
by haukex (Bishop) on Apr 05, 2017 at 16:08 UTC
    \@{ [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.


Re^3: Syntax for casting map to a hash or an array
by 1nickt (Abbot) on Apr 05, 2017 at 16:09 UTC

    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://1187144]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2021-01-23 09:04 GMT
Find Nodes?
    Voting Booth?