Even the fragment shows some of your problems... that is, it would, with use strict; use warnings and a few print statements to see what your vars actually hold:
#!/usr/bin/perl use 5.014; # functional equiv to use strict; use warnings; for t +his demo # 985557 my ($x, @array, $index, $arr_ref, @arr); sub sub1{ for $x (0 .. 3){ $array[$x] = 0; } say "\t from sub \$array[\$x]: |$array[$x]|"; # omit the part for generating the array; return \@array; } ####################### main part for ($index=0; $index<2; $index++){ $arr_ref = &sub1($index); $arr[$index] = @$arr_ref; foreach $_ (0 .. 3){ say "\@arr | @arr |, \$index: |$index|, \$_: |@_|"; # print "$arr[$index][$_]\n"; # Can't use string ("101") as an ARRAY ref while "strict refs" # in use at 985557.pl line 24. } say "\@arr | @arr |, \$index: |$index|, \$_: |@_|"; } say "\@arr | @arr |, \$index: |$index|, \$_: |@_|";
No, this won't bork your system; TITS -- " ...try it to see!"
And for your next step toward mastery, learning the debugger will be a help; better, in most cases than sticking in print statements, willy-nilly, as above.
In reply to Re: Question about how to return a array in subroutine to form a two dimensional array
by ww
in thread Question about how to return a array in subroutine to form a two dimensional array
by mianfeidewucan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |