in reply to Returning Multidimensional array from subroutine to main code ;

I would pass the array by reference. As an added bonus, you don't have to return it, as the subroutine will update the original array directly. (This is not always a good thing but probably is desired here.)

Example:

my @array = (); # ... populate array here &multi(\@array); # ... later, the subroutine: sub multi { my $arrayReference = shift; my @localArray = @$arrayReference; # ... do stuff to @localArray # ... it actually affects the original array }

A more full demonstration is hidden behind the readmoretag:

#!/usr/bin/perl use strict; { my @originalArray = (); print "-----[ initArrayLocally() ]---------------\n"; my $initializedArrayReference = &initArrayLocally(\@originalArray) +; my @array = @$initializedArrayReference; print "Value of \@array[5][5][5] = '$array[5][5][5]'\n"; print "-----[ modifyArrayLocally() ]---------------\n"; my $newArrayReference = &modifyArrayLocally(\@array); my @newArray = @$newArrayReference; print "Value \@array[5][5][5] = '$array[5][5][5]'\n"; print "Value \@newarray[5][5][5] = '$newArray[5][5][5]'\n"; print "-----[ modifyArrayByReference() ]---------------\n"; &modifyArrayByReference(\@array); print "Value \@array[5][5][5] = '$array[5][5][5]'\n"; print "-----[ Done ]---------------\n"; } sub initArrayLocally { my $arrayReference = shift; my @localArray = @$arrayReference; my $value = 0; # OMG This looks like FORTRAN or BASIC from 35 years ago! for (my $i=0; $i<10; $i++) { for (my $j=0; $j<10; $j++) { for (my $k=0; $k<10; $k++) { $localArray[$i][$j][$k] = ++$value; } } } return \@localArray; } sub modifyArrayLocally { my $arrayReference = shift; my @localArray = @$arrayReference; $localArray[5][5][5] = 42; return \@localArray; } sub modifyArrayByReference { my $arrayReference = shift; ${$arrayReference}[5][5][5] = 99; return; } exit; __END__ C:\Steve\Dev\PerlMonks\P-2013-10-18@2257-SubRet-3DArray>perl ret3d2.pl -----[ initArrayLocally() ]--------------- Value of @array[5][5][5] = '556' -----[ modifyArrayLocally() ]--------------- Value @array[5][5][5] = '42' Value @newarray[5][5][5] = '42' -----[ modifyArrayByReference() ]--------------- Value @array[5][5][5] = '99' -----[ Done ]---------------