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

Hi,

All the time I've trouble with understanding references, dereferences ect.

I've got such structure of data:

push @{$obiekt{$a}{$b}}, [$c,$d];

Question is :how can I get $c and $d ?

BR Michał

Replies are listed 'Best First'.
Re: references, dreferences
by tybalt89 (Monsignor) on Nov 19, 2018 at 20:36 UTC
    #!/usr/bin/perl -l use strict; use warnings; my ($a, $b, $c, $d) = ('a' .. 'd'); my %obiekt; push @{$obiekt{$a}{$b}}, [$c,$d]; use Data::Dump 'dd'; dd \%obiekt; print $obiekt{$a}{$b}[0][0]; print $obiekt{$a}{$b}[0][1];
Re: references, dreferences
by AnomalousMonk (Archbishop) on Nov 19, 2018 at 22:36 UTC

    The | A general answer to the question "How can I get X?" from a complex data structure is to start from the top level and work downwards. The process may be a bit tedious, but will usually bear fruit.

    Consider:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -e "my ($a, $b, $c, $d) = ('a' .. 'd'); ;; my %obiekt; ;; push @{$obiekt{$a}{$b}}, [$c,$d]; dd \%obiekt; print qq{\n}; ;; dd $obiekt{$a}; print qq{$obiekt{$a} \n\n}; ;; dd $obiekt{$a}{$b}; print qq{$obiekt{$a}{$b} \n\n}; ;; dd $obiekt{$a}{$b}[0]; print qq{$obiekt{$a}{$b}[0] \n\n}; ;; print qq{>$obiekt{$a}{$b}[0][0]< >$obiekt{$a}{$b}[0][1]<}; " { a => { b => [["c", "d"]] } } { b => [["c", "d"]] } HASH(0x216fa0) [["c", "d"]] ARRAY(0x216fdc) ["c", "d"] ARRAY(0x21700c) >c< >d<
    The top level
        dd \%obiekt;
    yields
        { a => { b => [["c", "d"]] } }
    which may be a bit confusing in and of itself.

    Going down a level to
        dd $obiekt{$a};
        print qq{$obiekt{$a} \n\n};
    yields
        { b => [["c", "d"]] }
        HASH(0x216fa0)
    and tells you that the value of the  $obiekt{$a} key of the hash is yet another hash reference. print-ing this reference simply stringizes the reference into a standard format that tells you that a reference is present and the type of the reference; it's not useful for much else.

    Another level down, and
        dd $obiekt{$a}{$b};
        print qq{$obiekt{$a}{$b} \n\n};
    produces
        [["c", "d"]]
        ARRAY(0x216fdc)
    which tells you that you now have a reference to an array containing a single item: another array reference.

    Yet another level down, and
        dd $obiekt{$a}{$b}[0];
        print qq{$obiekt{$a}{$b}[0] \n\n};
    outputs
        ["c", "d"]
        ARRAY(0x21700c)
    which is the value of the lowest level array reference.

    Finally,
        print qq{>$obiekt{$a}{$b}[0][0]< >$obiekt{$a}{$b}[0][1]<};
    accesses the content of the lowest level array reference and gives you
        >c< >d<

    Standard Disclaimer Concerning $a And $b Special Variables:   These variable names are used for Perl special package variables (see General Variables section of perlvar) that are associated with the sort built-in and its ilk. These names should generally be avoided in your code, even brief example code such as in the OP, and especially when used as the name of a lexical. Severe foot distress may result otherwise.


    Give a man a fish:  <%-{-{-{-<

Re: references, dreferences
by AnomalousMonk (Archbishop) on Nov 19, 2018 at 21:12 UTC
Re: references, dreferences
by afoken (Chancellor) on Nov 19, 2018 at 20:47 UTC

    perlreftut

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)