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 ]---------------

In reply to Re: Returning Multidimensional array from subroutine to main code ; by marinersk
in thread Returning Multidimensional array from subroutine to main code ; by vels

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.