Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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 ( #985566=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

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; }
  • Comment on Re: Question about how to return a array in subroutine to form a two dimensional array
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985566]
help
Chatterbox?
[stonecolddevin]: Van Halen is still around but I haven't heard any of their new stuff
[erix]: oh, 41M views -- everyone has seen it already then :)
[erix]: Alvin Lee dead too -- we're stuck with Karl, I'm afraid :)
[erix]: ok, one to remember Alvin Lee (Ten Years After - Help Me)
stevieb has a significant craving to hear "My guitar gently weeps"...
[Corion]: stonecolddevin: I think Jack White is highly regarded, but ...
[Corion]: ... I'm not really knowledgeable about good guitar players
[stonecolddevin]: I've been on a Stevie Ray Vaughan kick lately: https://www. youtube.com/watch? v=wVjdMLAMbM0
[stonecolddevin]: Corion I haven't heard much of his work to be honest.
[erix]: here is a nice cover, stevieb

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (15)
As of 2017-06-22 21:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (531 votes). Check out past polls.