# count the number of element in an array that are greater than somevalues?

Discussion in 'Perl Misc' started by Peng Yu, Jun 19, 2010.

1. ### Peng YuGuest

I'm wondering what is the shortest code to count the number of element
in an array that are greater than some values. I'm just not familiar
with perl enough to know what is the best way.

A for-loop is the easiest way to think of.

I also come up with the following way. But I'm not sure how to simply
it in one line. I'm sure that there are better ways. Would you please
let me know so that I can understand the expressive power of perl?

@new_array=grep { \$_ > 5 } @array;
\$#new_array+1

Peng Yu, Jun 19, 2010

2. ### Jürgen ExnerGuest

Re: count the number of element in an array that are greater than some values?

Peng Yu <> wrote:
>I'm wondering what is the shortest code to count the number of element
>in an array that are greater than some values. I'm just not familiar
>with perl enough to know what is the best way.
>
>A for-loop is the easiest way to think of.
>
>I also come up with the following way. But I'm not sure how to simply
>it in one line. I'm sure that there are better ways. Would you please
>let me know so that I can understand the expressive power of perl?
>
>@new_array=grep { \$_ > 5 } @array;

Just use the scalar value of the return value of grep:
\$count = scalar (grep ......)

>\$#new_array+1

Why last array index + 1 instead of simply using @new_array in scalar
context? Using the scalar context is not only easier and cleaner, it is
also correct if someone was crazy enough to modify \$[ in which case your
approach would yield the wrong number.

jue

Jürgen Exner, Jun 19, 2010

3. ### Jürgen ExnerGuest

Re: count the number of element in an array that are greater than some values?

Jürgen Exner <> wrote:
>Peng Yu <> wrote:
>>I'm wondering what is the shortest code to count the number of element
>>in an array that are greater than some values. I'm just not familiar
>>with perl enough to know what is the best way.
>>
>>A for-loop is the easiest way to think of.
>>
>>I also come up with the following way. But I'm not sure how to simply
>>it in one line. I'm sure that there are better ways. Would you please
>>let me know so that I can understand the expressive power of perl?
>>
>>@new_array=grep { \$_ > 5 } @array;

>
>Just use the scalar value of the return value of grep:
> \$count = scalar (grep ......)

Should have mentioned it explicitely: This is of course the manual,
explicit version that you can always use if you can't think anything
smarter.
For grep() itself it is much simpler, just read the documentation of the
function you are using, in particular the last sentence in the second
paragraph.

>>\$#new_array+1

>
>Why last array index + 1 instead of simply using @new_array in scalar
>context? Using the scalar context is not only easier and cleaner, it is
>also correct if someone was crazy enough to modify \$[ in which case your
>approach would yield the wrong number.
>
>jue

Jürgen Exner, Jun 19, 2010
4. ### Dr.RuudGuest

Re: count the number of element in an array that are greater thansome values?

Peng Yu wrote:

> I'm wondering what is the shortest code to count the number of element
> in an array that are greater than some values. I'm just not familiar
> with perl enough to know what is the best way.
>
> A for-loop is the easiest way to think of.
>
> I also come up with the following way. But I'm not sure how to simply
> it in one line. I'm sure that there are better ways. Would you please
> let me know so that I can understand the expressive power of perl?
>
> @new_array=grep { \$_ > 5 } @array;
> \$#new_array+1

Per is a strongely typed language.
One type dimension is scalar-array/hash (context).
Another type dimension is enforced by operators (casting).

Example:

perl -wle '
my \$odds = grep \$_ % 2,
map rand(\$_),
( 314.15927 ) x 50;
print \$odds;
'
30

--
Ruud

Dr.Ruud, Jun 19, 2010