in reply to Re: pushing into arrays of arrays
in thread pushing into arrays of arrays

Thanks for the replies;

I have a problem with references in that I was specifically hoping to create a copy of the array. The code works a bit like this;
loop loop calculate @values end loop push @values into @sets for use later end loop

Replies are listed 'Best First'.
Re: Re: Re: pushing into arrays of arrays
by Crian (Curate) on Mar 05, 2004 at 12:37 UTC
    Just read the post from xdg above your post again. He just pointed out, that you could use copies in such a case using

    push @array, [ @localarray ]

    instead of

    push @array, \@localarray
      Ahhh! Doh! Thanks :)
      Hmm..,

      I'm using the following to read through all of the pushed arrays and list their lengths, but it is returning length -1 each time..
      foreach $ary (@arrays) { print $#{ary}; }


      Grr. :)

        1t Note: $#array is not the length, but the biggest index.

        2nd note:use strict

        #!/usr/bin/env perl use strict; my $ar1 = [1,2,3]; my $ar2 = [4,5,6,7]; my @arrays = ($ar1,$ar2); foreach my $ary (@arrays) { print $#{ary}, "\n"; } __END__ Global symbol "@ary" requires explicit package name at test.pl line 10 +. Execution of test.pl aborted due to compilation errors.

        3d note: Read perldata and perlref! Really, that helps a lot!

        4th note:

        #!/usr/bin/env perl use strict; use warnings; my $ar1 = [1,2,3]; my $ar2 = [4,5,6,7]; my @arrays = ($ar1,$ar2); foreach my $ary (@arrays) { # correct syntax print $#{@$ary}, "\n"; # correct length print scalar(@$ary), "\n"; }

        regards,
        tomte


        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.