Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by mianfeidewucan (Initiate)
on Aug 05, 2012 at 17:49 UTC ( #985557=perlquestion: print w/ replies, xml ) Need Help??
mianfeidewucan has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I want to return an array in a subroutine to form a two dimensional arrays in a main function. This is what I do:
sub sub1{ for $x (0 .. 100){ $array[$x] = 0; } # omit the part for generating the array; return \@array; } ####################### main part my $index; for ($index=0; $index<2; $index++){ $arr_ref = &sub1($index); $arr[$index] = @$arr_ref; foreach (0 .. 100){ print "$arr[$index][$_]\n"; } }
However, there is nothing printed out. I am not sure where I wrote it wrong. Could somebody help me? Thanks!

Comment on Question about how to return a array in subroutine to form a two dimensional array
Download Code
Re: Question about how to return a array in subroutine to form a two dimensional array
by Marshall (Prior) on Aug 05, 2012 at 18:36 UTC
    I would suggest that your present some input data and the desired output.

    There are multiple problems with this code.
    Start over and explain the problem.

    Update:
    Well I got "yelled at" - No offense was intended.
    However, I do that think that there is a valid question about what the ultimate object here is.

Re: Question about how to return a array in subroutine to form a two dimensional array
by ww (Bishop) on Aug 05, 2012 at 18:47 UTC

    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.

Re: Question about how to return a array in subroutine to form a two dimensional array
by ig (Vicar) on Aug 05, 2012 at 19:20 UTC

    Your immediate issue seems to be:

    $arr[$index] = @$arr_ref;

    @$arr_ref is the array returned by your subroutine. Evaluated in scalar context, it yields the number of elements in the array: 101.

    An array element cannot hold an array, but it can hold an array reference and your subroutine returns an array reference. If you change the line to:

    $arr[$index] = $arr_ref;

    You may have something more like what you were hoping for. You will certainly have some output other than newlines.

    When trying to discover why my data structures are not what I want or expect, I find Data::Dumper very helpful. In the case of your program, the following might help you:

    use Data::Dumper; sub sub1{ for $x (0 .. 100){ $array[$x] = 0; } # omit the part for generating the array; return \@array; } ####################### main part my $index; for ($index=0; $index<2; $index++){ $arr_ref = &sub1($index); $arr[$index] = @$arr_ref; die Dumper(\@arr); foreach (0 .. 100){ print "$arr[$index][$_]\n"; } }

    This gives the following output:

    $VAR1 = [ 101 ];

    You need to get familiar with Data::Dumper to understand this output, but it is Perl code. This shows that @arr was an array of one element having the value 101. Try removing the '@' as I suggested and see what you get.

    I note also that your subroutine is using the global array @array. Each time it runs it sets values in that array and returns a reference to it. As it is the same array each time, each reference is a reference to the same array. They are not independent, as you may be expecting.

    There are various ways you could return a unique array form each execution of your subroutine. I would probably do something like the following:

    sub sub1{ my @array; for $x (0 .. 100){ $array[$x] = 0; } # omit the part for generating the array; return \@array; }
Re: Question about how to return a array in subroutine to form a two dimensional array
by linuxkid (Sexton) on Aug 06, 2012 at 16:04 UTC

    the generator of the array looks a little off, personally i would have used a ref, i.e. $array = [];, and done it manually, or with a loop.

    --linuxkid


    imrunningoutofideas.co.cc

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985557]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-07-31 06:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (245 votes), past polls