in reply to Array of arrays

Try this: print @{$AoA[5]}[3]; or: print $AoA[5]->[3]; $AoA[n] contains a reference to an array. You have to dereference it before you can access the array elements. For more info see perllol.

Good luck!

Update: Hmm, only saw one problem and ignored the rest. Guess I was just after first post! ;) I still think dereferencing before accessing is better style (I had no idea it was optional for an array with 2 depth). Apologies, reading perllol now.

Replies are listed 'Best First'.
Re: Re: Array of arrays
by Fastolfe (Vicar) on Jan 22, 2001 at 09:54 UTC
    $AoA[5][3] is a perfectly valid way of referencing an element of the array reference in @AoA. Try it. Perhaps you should give perllol a quick once-over yourself :).
Re: Re: Array of arrays
by arhuman (Vicar) on Jan 22, 2001 at 18:00 UTC
    I thought $a[1][2] was equivalent to $a[1]->[2]
    (ie. the -> is implicit between 2 [][]) Am I wrong ?
    Could someone explain me the difference then ?
    (or even point me to the place I could get the info)

      You can find all the info you need at perlref and perllol. Here's a brief description, though.

      When you have a reference to something, there are two ways you can dereference it. By dereference I mean "take the pointer to an object and return the object." You can prefix the reference with the data type indicator or use the dereference operator. For example:

      my @array = ('cow', 'dog', 'cat'); my $ref_to_array = \@array; my ($first, $second) = @{$ref_to_array}[0,1]; # prefix my $third = $ref_to_array->[2]; # deref operator

      This really comes in handy when you have four levels deep of referencing and you need something at the bottom. Typically, the deref operator (->) is optional between multiple levels of dereferencing. For example:

      my @LoL = (); # empty array $LoL[0] = []; # ref to anonymous array $LoL[0]->[0] = []; # another ref to anonymous array # the hard way ${${$LoL[0]}[0]}[0] = 'Look, up in the sky!'; # the easy way $LoL[0]->[0]->[1] = 'It's a bird, it's a plane, it's...'; # the shortcut $LoL[0]->[0][2] = 'Superman!';

      Now that you have more information than you needed or wanted... In this particular example, Perl "Does What You Mean." When you write $LoL[5][3] Perl recognizes that there's no way to rewrite that to mean anything but what you meant to do in the first place. =) So it just assumes the shortcut.

      That's probably the worst example of anything I've ever given. Go read the perldocs! {g}

        Those are good examples of using references. I have one correction and one addition.
         

            my $first = @{$ref_to_array}[0]; # prefix That should be: my $first = ${$ref_to_array}[0]; # prefix Just like with a regular array, you should use $ when accessing a single element, and save @ for when you want a slice.
         

        # the easy way $LoL[0]->[0]->[1] = 'It's a bird, it's a plane, it's...'; # the shortcut $LoL[0]->[0][2] = 'Superman!';
        To finish the abbreviation:
        # the full shortcut $LoL[0][0][2] = 'Superman!';
Re: Re: Array of arrays
by cajun (Chaplain) on Jan 22, 2001 at 09:54 UTC
    Thanks, but both of your corrections failed in exactly the same way as before.