Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Question about how to return a array in subroutine to form a two dimensional array

by ww (Archbishop)
on Aug 05, 2012 at 18:47 UTC ( [id://985561]=note: print w/replies, xml ) Need Help??


in reply to Question about how to return a array in subroutine to form a two dimensional array

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://985561]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found