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 Yu

    Peng Yu Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. Peng Yu

    Dr.Ruud Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. falcon
    Replies:
    10
    Views:
    18,551
    Roedy Green
    Feb 24, 2006
  2. Jason
    Replies:
    25
    Views:
    964
    Jorge Rivera
    Feb 22, 2004
  3. Martin
    Replies:
    9
    Views:
    2,771
    Karl Heinz Buchegger
    Nov 15, 2004
  4. P.N.

    array greater than 100

    P.N., Mar 18, 2006, in forum: C Programming
    Replies:
    18
    Views:
    447
    Jordan Abel
    Mar 19, 2006
  5. Dwight Army of Champions
    Replies:
    4
    Views:
    2,702
    John H.
    Mar 17, 2010
Loading...

Share This Page