×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

array length

array length

array length

(OP)
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

RE: array length

(OP)
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

RE: array length

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

RE: array length

simplly doing this is fine:

$length = @array;

This should work just fine...

RE: array length

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

RE: array length

(OP)
Thanks guys,

That is even easier than my first thoughts! And it gives the correct number immediately.

RE: array length

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close