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

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

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


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

Wait, what part works, but for not the same reason?

These two work for exactly the same reason.

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

These two work but NOT for the same reason, because there's an extra layer of ref->hash->ref going on?
That's the difference you're talking about, right?

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

Thanks,
cbeckley

Replies are listed 'Best First'.
Re^7: Syntax for casting map to a hash or an array
by shmem (Chancellor) on Apr 05, 2017 at 19:07 UTC

    The first two work. In the upper, an anoymous hash is constructed with map's output, in the lower, an anonymous array. In each case, Dumper just get's passed the reference and dereferences it.

    These two work but NOT for the same reason, because there's an extra layer of ref->hash->ref going on?
    say Dumper(\@{ {map { $_ => 'fish' } qw(one two red blue)} } );

    This one doesn't work, because the code is trying to dereference the anonymous hash as an array (@{ }).

    The next one is just like the upper one from the former code block.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Ugh, right, that was a typo. I was trying to compare these two lines.

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

      Thank you for the correction.

      Thanks,
      cbeckley

        Yes, these two are exactly the same, except for one gratitous reference-dereference round trip.

        If you dereference a reference and take a reference to the dereferenced reference, this reference will be the same as the original reference.

        #!/usr/bin/perl -l $h = {map { $_ => 'fish' } qw(one two red blue)}; print $h; print \%{$h}; print \%{\%{$h}}; print \%{\%{\%{$h}}}; print \%{\%{\%{\%{$h}}}}; print \%{\%{\%{\%{\%{$h}}}}}; print \%{\%{\%{\%{\%{\%{$h}}}}}}; print \%{\%{\%{\%{\%{\%{\%{$h}}}}}}}; print \%{\%{\%{\%{\%{\%{\%{\%{$h}}}}}}}}; __END__ HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548) HASH(0x17ea548)

        Sorry for so may ref-deref, couldn't stop... %-)

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2020-05-29 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (167 votes). Check out past polls.

    Notices?