desertman has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to get my head around some code that has this statement:

push @{$xyz{$item[1]}{is}}, @{$item[3]};

something like there is a hash table %xyz, and they push the value from another hash table associated with they key $item3, and also what is {is}(?)

thanks.

desrtman

Replies are listed 'Best First'.
Re: unclear what this means
by BrowserUk (Patriarch) on Dec 19, 2009 at 11:45 UTC

    Does this equivalent code make things any clearer for you?

    my @temp = @{ $item[3] }; my $key = $item[ 1 ]; my $arrayref = $xyz{ $key }{ is }; push @{ $arrayref }, @temp;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: unclear what this means
by shmem (Chancellor) on Dec 19, 2009 at 15:37 UTC

    %xyz is a hash. In it, under the key stored in $item[1], lives a values which is an anonymous hash ( { } ). In that anonymous hash, under the key is, an anonymous array ([ ]) is stored, which is dererferenced with the construct @{ }. See perlref.

    To that anonymous array, the content of another anonymous array is pushed, which is stored at slot 4 of the array @item.

    Or, said as comments to the code,

    #!/usr/bin/perl use Data::Dumper; $Data::Dumper::Indent = 1; my (@item, %xyz); $item[1] = 'some key'; $item[3] = [ qw(foo bar quux) ]; push # push to an array - which is @{ # a dereferenced anonymous array which lives $xyz{ # in the hash %xyz, under the key stored in $item[1] # element 2 of the array @item } # inside that keys value which is an {is} # anonymous hash, there under the key 'is' }, # - push to that array - @{ # the contents of an anonymous array $item[3] # stored as element 4 in the array @item }; print Data::Dumper->Dump([\%xyz] , [qw( xyz )]); print Data::Dumper->Dump([\@item], [qw( item )]); __END__ $xyz = { 'some key' => { 'is' => [ 'foo', 'bar', 'quux' ] } }; $item = [ undef, 'some key', undef, [ 'foo', 'bar', 'quux' ] ];
Re: unclear what this means
by llancet (Friar) on Dec 20, 2009 at 01:56 UTC
    %xyz is a 3-dimensional data structure, whose outer 2 layers are hash, and inner layer is array. Thus, to access an element in %xyz, you say:
    my $value = $xyz {level1_key} {level2_key} [index];
    So, to push something into the inner array of %xyz, you say:
    push @{ $xyz {level1_key}{level2_key} }, $value;

    Moreover, @item is a 2-D array. To access an element, you say:
    my $value = $item[index1][index2];
    To get one inner dimension fully as array ref, you say:
    my $array_ref = $item[index1];
    To get one inner dimension fully, you say:
    my @values = @{ $item[index1] };

    So you may wonder: if say $xyz{$item[1]}, it is using an array ref's stringified name as a hash key. It is weird but might be ok, because perl will ensure that every ref's stringified name is unique.