Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

What is $x{$y}?

by invaderzard (Acolyte)
on Sep 10, 2012 at 18:54 UTC ( #992825=perlquestion: print w/ replies, xml ) Need Help??
invaderzard has asked for the wisdom of the Perl Monks concerning the following question:

Some codes i read have this. @{$x{$y}}

What does $x{$y} mean? And what does adding the @ in front do? Does it convert the $x{$y} into an array?

Thanks.

Comment on What is $x{$y}?
Re: What is $x{$y}?
by Riales (Hermit) on Sep 10, 2012 at 19:02 UTC

    It looks like you have a array @x of array refs, of which you want the array ref at index $y.

    You have a hash %x of array refs, of which you want the array ref with key $y.

    The @{ } that surrounds all of it means you're dereferencing the array ref at index $y and getting the array that the array ref points to.

      Or strictly speaking, a hash %x of array refs.

      Dave.

        Oops, you're right. Array is totally wrong. Making the change to the original post!
Re: What is $x{$y}?
by toolic (Chancellor) on Sep 10, 2012 at 19:04 UTC
Re: What is $x{$y}?
by Rudolf (Monk) on Sep 10, 2012 at 19:44 UTC

    Like it was said above, it is a dereference because a hash only holds scalar values which could be references to an array, hash, or another scalar.. any reference. $x{$y} grabs the reference which is your scalar that you must dereference to make sense out of it.

    Just like my $ref = \@ary; creates a reference to the array and the way you would dereference it would be either @$ref or @{$ref}. In the case of a complex variable such as $x{$y} it is probably easier and more readable (once you get used to it) to use surrounding curly braces.

Re: What is $x{$y}?
by invaderzard (Acolyte) on Sep 10, 2012 at 23:58 UTC
    Thanks guys.

    Just one more question. What about $x{@y}?

      Just one more question. What about $x{@y}?

      You tell us? Try It To See? Tutorials: Basic debugging checklist?

      I don't know what this means, do you have any idea?

      $ perl -MData::Dump -e " @x = 1..2; $y{@x} = @x; dd \%y " { 2 => 2 } $ perl -MData::Dump -e " @x = 1..4; $y{@x} = @x; dd \%y " { 4 => 4 }

      In the  $x{ @y } expression, array  @y is being evaluated in scalar context, i.e., it yields the number of elements of the array. This number is then stringized as a key for the hash.

      >perl -wMstrict -le "my %hash = qw(one uno two dos); my @y = 5 .. 8; ;; print 'scalar @y: ', scalar @y; print 'does not exist' if not exists $hash{ @y }; ;; $hash{ '4' } = 'four'; print 'does exist: ', qq{'$hash{ @y }'} if exists $hash{ @y }; " scalar @y: 4 does not exist does exist: 'four'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-11-26 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (166 votes), past polls