Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl: correctly print array of arrays (dereference)
    text
    copied!<p>Hey fellow perl monks,</p> <p>I'm still wrapping my head around how to correctly dereference. (I read the similar posts prior to posting, but unfortunately am still a bit cloudy on the concept.)</p> <p>I have the following array, which internally is composed of two arrays. (BTW, I am using strict and warning pragmas.)</p> <pre><code>use strict; use warnings; my @a1; my @a2; </code></pre> <p>where:</p> <pre><code>@a1 = ( "1MB", "2MB", ... ) </code></pre> <p>and..</p> <pre><code>@a2 = ( "/home", "/home/debug", ... ) </code></pre> <p>Both @a1 &amp; @a2 are arrays which contain 51 rows. So, I populate these into my 2nd array.</p> <pre><code>my @b; push (@b, [ @a1, @a2 ]); </code></pre> <p>However, when I try to print the results of @b: </p> <pre><code>sub newl { print "\n"; print "\n"; } my $an1; my @an1; $an1 = $#a1; @an1 = ( 0, 1..$an1 ); for my $i (@an1) { print @b[$i]; &amp;newl; } </code></pre> <p>I see references to the arrays:</p> <pre><code>ARRAY(0x81c0a10) . ARRAY(0x81c0a50) . . . </code></pre> <p>How do I properly print this array? I know I need to dereference the array, I'm not sure how to go about doing this. I tried populating my array as such:</p> <pre><code>push (@b, [ \@a1, \@a2 ]); </code></pre> <p>Which produces the same results. I also tried:</p> <pre><code>for my $i (@an1) { print @{$b[$i]}; &amp;newl; } </code></pre> <p>Which unfortunately errors due to having 0 as an array reference?</p> <pre><code>Can't use string ("0") as an ARRAY ref while "strict refs" in use at p_disk_ex6.pl line 42. </code></pre> <p>Any suggestions are greatly appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload