A Perl Chicken-and-egg problem

Seneca Cunningham tentra-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Sat Jun 21 19:05:43 UTC 2008


2008/6/21 Paul King <sciguy at vex.net>:
> my @A = myobj::files::list(params);
> foreach $x (@A) {
>    for ($i = 0; $i < 16; $i++) {
>         for $j = 0; $j < 16; $j++) {
>              print "$x->[$i][$j]\n";
>         }
>    }
> }
>
> This kind of works. I get the output I expect, except for one thing. I
> don't know the array size, so I am hard-coding the upper limit of the
> loops. $#x to find the size doesn't work, since (1) $x merely points to
> an array, and (2) the array is two dimensional. Any suggestions?

If you care about the indices, you could use the form:

  foreach my $x (@A) {
     foreach my $i (0 .. (@$x -1)) {
          foreach my $j (0 .. (@{$x->[$i]} - 1)) {
               print "$x->[$i][$j]\n";
          }
     }
  }

If you don't care about the indices, you could use the form:

  foreach my $x (@A) {
     foreach my $i (@$x) {
          foreach my $j (@$i) {
               print "$j\n";
          }
     }
  }

Both forms rely upon dereferencing the arrayrefs.  The perldoc page
"perlref" has more details about referencing and dereferencing values.

-- 
Seneca Cunningham
<tentra at gmail.com>


More information about the Legacy mailing list