Search code examples
perlperl-data-structures

How can i access the inlying properties?


I have a perl variable like this. How can i access the inlying properties (like '706')?

my @config = [
        {
        'x' => [ 565, 706 ],
        'y' => [ 122 ],
        'z' => 34,
        'za' => 59,
    }
];

EDIT: print Dumper($config[0]); yields : $VAR1 = undef;

Looks like i get acces using $config[0][0]->{x}[1];. Why do i have to use [0][0] (one is clear, but he ssecond...)?

EDIT2: I am sorry for changing the data structure, but the definition which was given to me changed.


Solution

  • [EDIT: Follow the shifting problem definition.]

    Given:

    my @config = ( 
      [
        { # NB: insertion order ≠ traversal order
            "x"  => [ 565, 706 ],
            "y"  => [ 122 ],
            "z"  => 34,
            "za" => 59,
        },
      ],
    );
    

    Then this will do it:

    # choice §1
    print $config[0][0]{"x"}[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element
    

    understanding of course that that is merely syntactic sugar for:

    # choice §2
    print $config[0]->[0]->{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element
    

    and that that is just syntactic sugar for:

    # choice §3
    print ${ $config[0] }[0]->{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element
    

    which in turn is just syntactic sugar for:

    # choice §4
    print ${ ${ $config[0] }[0] }{"x"}->[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element
    

    which again is syntactic sugar for:

    # choice §5
    print ${ ${ ${ $config[0] }[0] }{"x"}}[-1];   # get 1ˢᵗ row’s xᵗʰ row’s last element
    

    and that, of course, is equivalent to:

    # choice §6
    print ${ ${ ${ $config[0] }[0] }{"x"} }[ $#{ ${ ${ $config[0] }[0] }{"x"} } ];   # get 1ˢᵗ row’s xᵗʰ row’s last element