searching for a special string in an array

S

Shai

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.
 
J

Josef Moellers

Shai said:
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
 
A

anno4000

Shai said:
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
 
J

J?rgen Exner

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

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top