> -----Original Message-----
> From: Eko Budiharto [mailto:eko.budiha...@gmail.com]
> Sent: Friday, March 30, 2012 6:33 AM
> To: beginners@perl.org
> Subject: [question] array
> 
> hi list,
> I would like to ask about 2 dimensional array
> 
> my code:
> my $ref = $sth->fetchall_arrayref();
> foreach my $row (@$ref) {
>      ( $KODE_UNIT, $KODE_BAGIAN, $NIK, $TANGGAL ) = @$row;
>      @row = @$row;
>      push(@array_2d, @row  );
> 
>      print "<tr>";
> print "<td align=center valign=middle
> width=>@array_2d[0][0]<br>@array_2d[0][1]<br>@array_2d[0][2]</td>";
> print "</tr>";
> 
> }
> 
> 
> The result that I expected:
> @array_2d[0][0]
> @array_2d[0][1]
> @array_2d[0][2]
>       @array_2d[1][0]
> @array_2d[1][1]
> @array_2d[1][2]
>       @array_2d[2][0]
> @array_2d[2][1]
> @array_2d[2][2]
> 
> 
> I tried a few ways, I still get an error message.
> 
> I am kind of confused with 2D arrays. Please help. Thanks.

http://www.perlmonks.org/?node_id=90647

Take a look at the above reference.
Essentially, to create a two-dimensional array, you need to store have an
array of array references.
Rather than
>      @row = @$row;
>      push(@array_2d, @row  );

do this:

>      push(@array_2d, $row  );

Also, on the print statement, you are incorrectly using the array sigil (@).
> print "<td align=center valign=middle
> width=>@array_2d[0][0]<br>@array_2d[0][1]<br>@array_2d[0][2]</td>";
> print "</tr>";

To access an element of an array you need the scalar sigil ($):
$array_2d[0][0]

HTH, Ken




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to