Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the length of an array within a Perl hash

Tags:

perl

I have the following:

$data{host} -> [$i] -> {someotherstuff}

How can I get the length of the array where [$i] is?

like image 522
Magicked Avatar asked May 20 '10 16:05

Magicked


2 Answers

$length = scalar( @{ $data{host} } );
like image 150
Cfreak Avatar answered Sep 17 '22 16:09

Cfreak


If you want the last index, you can use: $#{ $data{host} }

Obviously, the length of the array is last index + 1. Use this notation when it is harder to achieve scalar context, or when you specifically want length-1. For example:

0..$#{$data{host}} # returns a list of all indices of the array

Sometime useful.

like image 33
Uri Avatar answered Sep 20 '22 16:09

Uri