searching for a special string in an array

Discussion in 'Perl Misc' started by Shai, Jul 10, 2007.

  1. Shai

    Shai Guest

    Hi,

    I'm running on an array of strings with a foreach loop.
    I want to stop the loop when I find the string: "item(s)" - but it
    looks like I have problems with defining the if condition.
    I tried:
    if ($str=~"item(s)") {
    last;
    }
    But it seems to have problems. if I remove the "(s)" it works fine but
    I need to find a unique string which contains exactly this: "item(s)".
    Any idea ?!?!?

    Thanks,
    Shai.
    Shai, Jul 10, 2007
    #1
    1. Advertising

  2. Shai wrote:
    > Hi,
    >
    > I'm running on an array of strings with a foreach loop.
    > I want to stop the loop when I find the string: "item(s)" - but it
    > looks like I have problems with defining the if condition.
    > I tried:
    > if ($str=~"item(s)") {
    > last;
    > }
    > But it seems to have problems. if I remove the "(s)" it works fine but
    > I need to find a unique string which contains exactly this: "item(s)".
    > Any idea ?!?!?


    The parentheses are special, they are used to capture part of the match:

    my $str = 'items';
    if ($str =~ "item(s)") {
    print "match: \$1=$1\n";
    }

    BTW Better stick with the usual "/" characters to delimit patterns:

    my $str = 'item(s)';
    if ($str =~ /item\(s\)/) {
    print "Match!!\n";
    }

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Jul 10, 2007
    #2
    1. Advertising

  3. Shai

    -berlin.de Guest

    Shai <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I'm running on an array of strings with a foreach loop.
    > I want to stop the loop when I find the string: "item(s)" - but it
    > looks like I have problems with defining the if condition.
    > I tried:
    > if ($str=~"item(s)") {


    Don't specify a regular expression as a string. While Perl does what
    you mean and interprets it as a regex, it's confusing and less robust
    than it could be.

    > last;
    > }
    > But it seems to have problems. if I remove the "(s)" it works fine but
    > I need to find a unique string which contains exactly this: "item(s)".


    If you're only looking for a substring you can use index():

    if ( index( $str, 'item(s)') >= 0 ) { # ...

    In a regex, the parens have special meaning and must be escaped:

    if ( $str =~ /items\(s\)/ ) { # ...

    Anno
    -berlin.de, Jul 10, 2007
    #3
  4. Shai

    J?rgen Exner Guest

    Shai wrote:
    > I'm running on an array of strings with a foreach loop.
    > I want to stop the loop when I find the string: "item(s)" - but it
    > looks like I have problems with defining the if condition.
    > I tried:
    > if ($str=~"item(s)") {


    It would be less confusing to you and others if you used the customary /.../
    for REs.
    Double quotes are a particularly poor choice because naturally people will
    confuse the text with a string.

    > last;
    > }
    > But it seems to have problems. if I remove the "(s)" it works fine but


    You need to escape the parantheses because they are using for grouping in
    REs.

    > I need to find a unique string which contains exactly this: "item(s)".
    > Any idea ?!?!?


    Even better: use the right tool. If you want to search for a substring
    within another string then index() is your friend. No need to wield the big
    RE cannon for that task.

    jue
    J?rgen Exner, Jul 10, 2007
    #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. diffused
    Replies:
    9
    Views:
    708
    Oscar kind
    Aug 1, 2004
  2. John_Woo
    Replies:
    4
    Views:
    653
    shakah
    Jul 6, 2006
  3. Peng Yu
    Replies:
    3
    Views:
    206
    C.DeRykus
    Jul 13, 2010
  4. Replies:
    2
    Views:
    412
  5. stumblng.tumblr
    Replies:
    1
    Views:
    186
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page