CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
2D array length (again!)

 



LerdoPerlo
New User

Feb 22, 2010, 9:13 AM

Post #1 of 4 (564 views)
2D array length (again!) Can't Post

Hi,

I'm sure this has been asked several times before, but I can't seem to find a proper answer.

Say I have the following two-dimensional array:


Code
@A = ( [00, 01, 02, 03], 
[10, 11, 12, 13, 14, 15, 15, 17, 18],
[20, 21, 22, 23, 24, 25, 26, 27],
[30, 31],
[40],
[50, 51, 52]
);


I've found that if I print the size of @A using:


Code
print "size of A: " . @A . "\n";


I get the expected value: 6

But if I try to find the size of each of the arrays within @A and I go further than its real size, that is:

Code
print "size of A[0]: " . $#{$A[0]} . "\n"; 
print "size of A[1]: " . $#{$A[1]} . "\n";
print "size of A[2]: " . $#{$A[2]} . "\n";
print "size of A[3]: " . $#{$A[3]} . "\n";
print "size of A[4]: " . $#{$A[4]} . "\n";
print "size of A[5]: " . $#{$A[5]} . "\n";
print "size of A[6]: " . $#{$A[6]} . "\n";
print "size of A[7]: " . $#{$A[7]} . "\n";
print "size of A[8]: " . $#{$A[8]} . "\n";


The next time I print the size of @A, it says it's now 9 elements big.

Furthermore: How one would add a new column in the last row (ie, an element to the last array of @A)? I was expecting this to work, but didn't:


Code
$A[ @A -1 ][ $#{$A[ @A -1 ]} ] = "new element";



toolic
User

Feb 22, 2010, 10:05 AM

Post #2 of 4 (558 views)
Re: [LerdoPerlo] 2D array length (again!) [In reply to] Can't Post

Each of the last 3 prints (6-8) increased the array size because of autovivification.

One way to add an element is to index the last row using -1, then do a push:

Code
push @{ $A[-1] }, "new element";



shawnhcorey
Enthusiast


Feb 22, 2010, 1:35 PM

Post #3 of 4 (553 views)
Re: [LerdoPerlo] 2D array length (again!) [In reply to] Can't Post

You should use a loop to access the contents of @A:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my @A = ( [00, 01, 02, 03],
[10, 11, 12, 13, 14, 15, 15, 17, 18],
[20, 21, 22, 23, 24, 25, 26, 27],
[30, 31],
[40],
[50, 51, 52]
);

print "The size of \@A is ", scalar( @A ), ".\n";
for my $i ( 0 .. $#A ){
print "The size of \@{\$A[$i]} is ", scalar( @{$A[$i]} ), ".\n";
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


LerdoPerlo
New User

Feb 23, 2010, 5:10 AM

Post #4 of 4 (543 views)
Re: [shawnhcorey] 2D array length (again!) [In reply to] Can't Post

Thank you very much, guys! With your information I've achieved what I needed. THANKS! Wink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives