Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array length

Status
Not open for further replies.

rtb1

IS-IT--Management
Jul 17, 2000
73
ES
Hi,

I couldn't find this in the Perl documentation but it must me possible.

How can I get the length of an array?

$length =@array.length; # returns 00?

What is the right syntax?

regards,

Raoul
 
sorry, miss typed array name.

but apparantly a '0' is added to the actual number. When the length is 5 it returns 50.

Is there a reason for this?

raoul
 
Try this....

@array = ('1','2','3','4,'5');
$length = $#array;
print "$length\n"; # prints 4 which is the fifth element starting with zero.


'hope this helps....




keep the rudder amid ship and beware the odd typo
 
simplly doing this is fine:

$length = @array;

This should work just fine...
 
damann is correct. I was making it harder than it needed to be. Thanks, damann.




keep the rudder amid ship and beware the odd typo
 
Thanks guys,

That is even easier than my first thoughts! And it gives the correct number immediately.
 
To be sure to get a REAL, you can also use SCALAR, eg:

$length = scalar(@array);

In an if statement, you could write:

if ( scalar(@array) > 1 ) {
.......
}


Thierry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top