TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

All,

I have a array reference, which points to other arrays. What I would like to do is to add more data to the array before I print it out.

print code:

for my $col (@array) { for my $row (@$col) { print "$row\n"; } }

say I have a new array

my @new_array = qw ( foo bar foobar barfoo );
How do I add that to array @array ? I'm probably overlooking over something easy. I know I can't use 'push' because the elements of @array are references.

Any help would be appreciated.

Replies are listed 'Best First'.
Re: Adding elements to a array reference
by Abigail-II (Bishop) on Jan 15, 2004 at 15:22 UTC
    I know I can't use 'push' because the elements of @array are references.
    Huh? push doesn't care what the elements of the array are. If you want to add elements to an array, just push with the name of the array as first argument:
    push @array => @new_array; # Adds four elements to @array push @array => \@new_array; # Adds one element to @array, # it being a reference to @new_array. push @array => [@new_array];# Adds one element to @array, # it being a reference to a copy of @n +ew_array.

    Abigail

      got it. Thanks.
Re: Adding elements to a array reference
by Roy Johnson (Monsignor) on Jan 15, 2004 at 15:27 UTC
    I think you meant for your inner loop to be for my $row (@$col).

    Just push(@array, \@new_array) if you want it to literally refer to @new_array, or push(@array, [@new_array]) if you want to copy it in.


    The PerlMonk tr/// Advocate
      ahh, yes I did mean that. Thanks a bunch.