• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

getting amount of elements of perl default array

Hi,

I can use $#array+1 to get the amount of elements in @array.

How about @_? Is it possible?

Thanks.
0
chainfear
Asked:
chainfear
2 Solutions
 
ozoCommented:
$amount_of_elements = $#_+1;
#or
$amount_of_elements = @_;
0
 
wilcoxonCommented:
You can always use @var to get the number of elements in scalar context.  What ozo has given is correct (as usual).  A simple equivalent way to make it explicit is:
$amount_of_elements = scalar @_;

Open in new window


To get the last element in a list, you can do:
$last = $arr[$#arr];
# or
$last = $arr[@arr-1];
# or
$last = $arr[-1];

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now