in reply to Returning Multidimensional array from subroutine to main code ;
Hello vels, and welcome to the Monastery!
Here is some simple code to create a 3D array, pass it into and out of a subroutine, and print it using the module Data::Dump:
#! perl use strict; use warnings; use Data::Dump; my @array3d = ( [ [ 'a', 'c', 'e', ], [ 'f', 'h', 'j', ], [ 'k', 'm', 'o', ], ], [ [ 'A', 'C', 'E', ], [ 'F', 'H', 'J', ], [ 'K', 'M', 'O', ], ], ); dd \@array3d; my $array_ref = multi(\@array3d); dd $array_ref; print "$array_ref->[0][2][1]\n"; $array_ref->[0][2][1] = 'Change 2'; dd $array_ref; sub multi { my ($array3d_ref) = @_; $array3d_ref->[1][1][2] = 'Change 1'; return $array3d_ref; }
Output:
13:12 >perl 749_SoPW.pl [ [["a", "c", "e"], ["f", "h", "j"], ["k", "m", "o"]], [["A", "C", "E"], ["F", "H", "J"], ["K", "M", "O"]], ] [ [["a", "c", "e"], ["f", "h", "j"], ["k", "m", "o"]], [["A", "C", "E"], ["F", "H", "Change 1"], ["K", "M", "O"]], ] m [ [["a", "c", "e"], ["f", "h", "j"], ["k", "Change 2", "o"]], [["A", "C", "E"], ["F", "H", "Change 1"], ["K", "M", "O"]], ] 13:12 >
Note the -> in the expression $array3d_ref->[1][1][2]. This is required; see:
Also, please put your code within <code> ... </code> tags, as this makes it easier for the monks to read.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|