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.
chainfearAsked:
Who is Participating?
 
wilcoxonConnect With a Mentor Commented:
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
 
ozoConnect With a Mentor Commented:
$amount_of_elements = $#_+1;
#or
$amount_of_elements = @_;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.