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: Intermediate:
array length?

 



mike_robb
Novice

Jul 23, 2001, 1:25 PM

Post #1 of 6 (1019 views)
array length? Can't Post

if I have an array @X
how can i print the length?

print "Length=".??."\n";

-TIA!



mhx
Enthusiast / Moderator

Jul 23, 2001, 1:30 PM

Post #2 of 6 (1018 views)
Re: array length? [In reply to] Can't Post


Code
print "Length=".scalar(@X)."\n";

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



mike_robb
Novice

Jul 23, 2001, 1:41 PM

Post #3 of 6 (1016 views)
Re: array length? [In reply to] Can't Post

Thank you -I'll give that a go.

(nice little signature :-)



S_Shrum
User

Jul 25, 2001, 3:57 PM

Post #4 of 6 (1001 views)
Re: array length? [In reply to] Can't Post

There is another way as well...

I deal with multi-dim arrays all the time. Sometimes I want the array row count and other times I want the array column count.

Row count: $#X

This will return the number of rows (starting at 0) that the array has. For display to a user, just add one to the result.

Column count: $#{$X[0]}

This will return the number of columns in the first row (0).

If you are going to traverse arrays, these are important to know as you can now tell the script how deep and how far across to go.

Sean Shrum
sean@shrum.net
www.shrum.net


mhx
Enthusiast / Moderator

Jul 25, 2001, 9:48 PM

Post #5 of 6 (996 views)
Re: array length? [In reply to] Can't Post

Evaluating in scalar context works for multidimensional arrays as well, and saves you from keeping in mind that you have to add '1' all the time...

Code
#!/bin/perl -w 
use strict;

my @X = (
[1, 2],
[3, 4],
[5, 6],
);

printf "Rows: %d\n", scalar @X;
printf "Cols: %d\n", scalar @{$X[0]};

In a scalar assignment, you can even shorten this to

Code
my $rows = @X; 
my $cols = @{$X[0]};

Of course, all this relies heavily upon the fact that all rows have the same number of columns. Since there are no 'real' multidimensional arrays in Perl as there are in C, you always have to keep in mind that you actually have an array of array references.
Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



dsb
stranger

Jul 26, 2001, 8:56 AM

Post #6 of 6 (993 views)
Re: array length? [In reply to] Can't Post

S_Shrum,
Technically, the '$#' returns the number of the last index of the array. You were right on about having to add 1 since the "count" starts from 0.

For purposes of finding array length, I'd go with the 'scalar' method.

dan 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