Prev: MIME::Lite and SMTPS (smtp.gmail.com)
Next: FAQ 4.69 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays?
From: Dr.Ruud on 19 Jun 2010 05:26 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 |