CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [BillKSmith] 2d array in sub: Edit Log



FishMonger
Veteran / Moderator

Sep 26, 2013, 7:25 AM


Views: 6812
Re: [BillKSmith] 2d array in sub

Another problem is that the array is being copied inside the sub, so any changes made to that new array are not reflected in the original array.

Solution is to not copy the array.

Code
use Data::Dumper; 

my @row1 = [0,1,2];
my @row2 = [3,4,5];
my @row3 = [6,7,8];

push @array_2d, @row1;
push @array_2d, @row2;
push @array_2d, @row3;

print "prior to sub\n", Dumper(\@array_2d), "\n";

displayarray(\@array_2d);
print "after sub\n", Dumper(\@array_2d);

sub displayarray
{
my $array = shift;
push $array, [3,2,1];

#print "size: $#array\n";
#for my $i (0..$#array){
# print "row: $i, size: $#{$array[$i]}, ";
# for my $j (0..$#{$array[$i]}){
# print "$array[$i][$j] ";
# }
# print "\n";
#}
}


On a side note, I'd use perl's for loop syntax instead of the C style syntax because it's cleaner and more efficient.


(This post was edited by FishMonger on Sep 26, 2013, 7:26 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Sep 26, 2013, 7:26 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives