Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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?


Replies are listed 'Best First'.
Re: What is $x{$y}?
by toolic (Bishop) on Sep 10, 2012 at 19:04 UTC
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.


        Oops, you're right. Array is totally wrong. Making the change to the original post!
Re: What is $x{$y}?
by Rudolf (Pilgrim) 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}?

      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'

      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 }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://992825]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2017-04-29 07:56 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.