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

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.