Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of 2D array in Perl

I need to get the size of the second level in a 2D array. I'm trying this:

my @txt;
$txt[0][0]="text1";
$txt[0][1]="text2";
$txt[0][2]="text3";

$txt[1][0]="text4";
$txt[1][1]="text5";
$txt[1][2]="text6";

print scalar(@txt[1]);

But it doesn't work, and I see "ARRAY(0x804daf0)". How to get the size of the second dimension?

like image 936
BArtWell Avatar asked May 28 '13 13:05

BArtWell


People also ask

How do I declare a 2d array in Perl?

Note Technically, two-dimensional arrays in Perl are arrays of arrays. Each "row" is itself a reference to the anonymous array in brackets. To refer to an element in a two-dimensional array, specify the array variable as a scalar with two indexes indicating the row and column of the element you are referring to.

How do I create a dynamic array in Perl?

Creating an array In Perl variables are identified using sigils. Arrays use @ (as in 'a' for array), so the format is: @any_name_you_choose_here. Arrays are initialised by assigning a list of values (comma separated values between parentheses).


1 Answers

print scalar @{ $txt[1] }; should do the trick...

like image 155
pavel Avatar answered Sep 18 '22 22:09

pavel