Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Adding elements to a array reference

by TASdvlper (Monk)
on Jan 15, 2004 at 15:15 UTC ( [id://321572]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://321572]
Approved by xenchu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2024-03-28 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found