in reply to pushing into arrays of arrays

Previous post about using references was right on. As a side note, though, if you're pushing an array that you reuse in a loop, you'll want to create a new, anonymous array reference each time through. E.g.

while ( @array = somefunc() ) { push( @aoa, [ @array ] ); }

To your second question, yes, that should work.

-xdg

Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Replies are listed 'Best First'.
Re: Re: pushing into arrays of arrays
by Anonymous Monk on Mar 05, 2004 at 12:24 UTC
    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
      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. :)