Conditional match similar to IDL's where function

Discussion in 'Perl Misc' started by vorticitywolfe@gmail.com, Feb 9, 2008.

  1. Guest

    Hello,

    I'm been trying to produce a result similar to IDL's where
    function...that returns the indices of where a condition is true.

    For example:
    @array=(2,4,7,9,10,16,20,82);
    print where(@array < 20 and @array >= 10);

    this should print 4,5 indicating that at index 4 and index 5 this
    condition is true.

    previous posts always match if something exists in a string, I want to
    mathematically check a condition
    >>"What are the indices of elements that match?", you'll need something like
    >> @indices = grep $array[$_]=~/something/, 0..$#array;


    Do you have any ideas for me?

    Thanks for the help!

    Jonathan
     
    , Feb 9, 2008
    #1
    1. Advertising

  2. <> wrote:
    > Hello,
    >
    > I'm been trying to produce a result similar to IDL's where
    > function...that returns the indices of where a condition is true.
    >
    > For example:
    > @array=(2,4,7,9,10,16,20,82);
    > print where(@array < 20 and @array >= 10);
    >
    > this should print 4,5 indicating that at index 4 and index 5 this
    > condition is true.
    >
    > previous posts always match if something exists in a string, I want to
    > mathematically check a condition



    You want to use arithmetic operators rather then the pattern match operator.


    >>>"What are the indices of elements that match?", you'll need something like
    >>> @indices = grep $array[$_]=~/something/, 0..$#array;

    >
    > Do you have any ideas for me?



    perldoc -f grep

    says:

    grep BLOCK LIST
    grep EXPR,LIST

    The code you quoted above uses the EXPR form.

    Simple replace the pattern match EXPR with an arithmetic EXPR:

    @indices = grep $array[$_] < 20 && $array[$_] >= 10, 0..$#array;

    I would prefer the BLOCK form for this instead though:

    @indices = grep {$array[$_] < 20 and $array[$_] >= 10} 0..$#array;



    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Feb 11, 2008
    #2
    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. Pushya

    Java and Rsi IDL

    Pushya, Jun 14, 2004, in forum: Java
    Replies:
    1
    Views:
    557
    Nigel Wade
    Jun 15, 2004
  2. Ken

    ADA/C++/IDL to XML?

    Ken, Sep 5, 2003, in forum: XML
    Replies:
    0
    Views:
    667
  3. Generic Usenet Account

    IDL Vs WSDL ---- a comparison

    Generic Usenet Account, May 27, 2004, in forum: XML
    Replies:
    24
    Views:
    2,523
    Brian Kelly
    Jun 13, 2004
  4. Holger Fleckenstein

    sockets: C++ server, IDL client

    Holger Fleckenstein, Sep 28, 2004, in forum: C++
    Replies:
    0
    Views:
    655
    Holger Fleckenstein
    Sep 28, 2004
  5. John Butler

    regex to match similar strings

    John Butler, Jul 12, 2010, in forum: Ruby
    Replies:
    2
    Views:
    235
    John Butler
    Jul 12, 2010
Loading...

Share This Page