in reply to Re: appending and array into last element of an array?
in thread appending and array into last element of an array?

OK. that sounds good so far, but if I'm doing this in a looping context, and the next time around the loop, the values of @arrayB change, won't that change all of the references as well?

Also, how could I save the anon array in your second example? setting something equal to the push gives the length of the new array, right? Could I just do

push(@arrayA, [ @arrayB ]); @new_array = @_;

would that work?

And thanks for the quick reply.
Matt

Replies are listed 'Best First'.
Re^3: appending and array into last element of an array?
by ikegami (Patriarch) on Oct 11, 2006 at 16:38 UTC

    OK. that sounds good so far, but if I'm doing this in a looping context, and the next time around the loop, the values of @arrayB change, won't that change all of the references as well?

    It depends on where @arrayB is declared.

    my @arrayA; my @arrayB; while (...) { @arrayB = ...; push(@arrayA, \@arrayB); # XXX }
    my @arrayA; my @arrayB; while (...) { @arrayB = ...; push(@arrayA, [ @arrayB ]); # Ok }
    my @arrayA; while (...) { my @arrayB = ...; push(@arrayA, \@arrayB); # Ok }
    my @arrayA; while (...) { my @arrayB = ...; push(@arrayA, [ @arrayB ]); # Needlessly creating 3rd array. }

    Also, how could I save the anon array in your second example?

    What does "save" mean? Copy? You already have a copy of the array in @arrayB.