in reply to Multi dimensional arrays

You need to access the proper element and dereference it.

use strict; use warnings; my @array = ("w", "h", [1, 2, "c"]); print $array[0],$/; # w print $array[1],$/; # h print $array[2],$/; # ARRAY(0x177f07c) You've found the array referenc +e! print ${$array[2]}[0],$/; # 1 print ${$array[2]}[1],$/; # 2 print ${$array[2]}[2],$/; # c

See tye's References quick reference for more details!

Replies are listed 'Best First'.
Re: Re: Multi dimensional arrays
by Hofmator (Curate) on Feb 08, 2003 at 11:28 UTC
    You need to access the proper element and dereference it.
    Yes, but why in such a difficult way?
    print ${$array[2]}[0],$/; # 1
    can be written as print $array[2]->[0], $/; where you can then even leave out the '->' as it stands between two subscripts, leading to print $array[2][0], $/;

    -- Hofmator

      Just because it can be written that way doesn't mean that we should teach it that way. It is much better for someone to learn how it works before they learn the shortcuts.
        I agree with you, but I don't consider $array[2]->[0] as a shortcut. It's a imho more readable alternative writing of your ${$array[2]}[0]. Perl has enough line noise as it is, so I don't see any point in promoting this :)

        Of course, it is a good thing to explain what $array[2]->[0] does, e.g. by pointing to tye's tutorial, exactly as you have done.

        The last shortcut to arrive at $array[2][0] is not necessary but why not mention it while we are at the topic. This abbreviation was introduced so that it looks like we are straightforwardly accessing a 2D array - which on purpose hides the 'gory details'. Once you have understood the underlying concepts, you don't have to deal with them explicitly any more ... that's Perl.

        -- Hofmator