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

P

Peng Yu

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
 
J

Jürgen Exner

Peng Yu said:
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

Jürgen Exner

Jürgen Exner said:
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
 
D

Dr.Ruud

Peng said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top