add match condition

  • Thread starter Mikael Petterson
  • Start date
M

Mikael Petterson

Hi,

In my perl script I have the following match:


if ($_ =~ m/$featureId/)

But I also want to add a condition saying:

not matching the string 'disable' in $_

How can I do that?

cheers,

//mikael
--
Mikael Petterson
Software Designer


Ericsson AB, Stockholm, Sweden
Visiting address: Isafjordsgatan 15, Kista
Phone: +46 70 2673044
E-mail: (e-mail address removed)
 
J

Josef Moellers

Mikael said:
Hi,

In my perl script I have the following match:


if ($_ =~ m/$featureId/)

But I also want to add a condition saying:

not matching the string 'disable' in $_

How can I do that?

$_ !~ /disable/
 
B

Ben Morrow

Quoth (e-mail address removed):
In my perl script I have the following match:


if ($_ =~ m/$featureId/)

But I also want to add a condition saying:

not matching the string 'disable' in $_

How can I do that?

Err... did you try

if ($_ =~ m/$featureId/ and not $_ =~ m/disable/) {

? Note that unless $featureId contains a regular expression, the first
match should be written

m/\Q$featureId/

which will ensure any special characters that find their way in there
are *not* treated specially.

You may feel it is clearer to explicitly include the '$_ =~' and 'm'
parts of the match, and if you are writing your code for yourself or
others unfamiliar with Perl that's OK. However, if you are planning to
become reasonably proficient, it's well worth getting used to Perl's
defaults. Putting in redundant information can be confusing: I had to
read that line twice and then translated it to 'if (/$featureId/)' in my
head, just because there's so much extra garbage around the important
bit of the test.

Ben
 
J

Jürgen Exner

Mikael Petterson said:
In my perl script I have the following match:

if ($_ =~ m/$featureId/)

But I also want to add a condition saying:

not matching the string 'disable' in $_

if (m/$featureId/ and index($_, 'disable') == -1)

This is assuming that $featureId actually does contain a regex. Otherwise it
would be better to replace the match with an index() call, too.

jue
 
M

Michele Dondi

if (m/$featureId/ and index($_, 'disable') == -1)

This is assuming that $featureId actually does contain a regex. Otherwise it
would be better to replace the match with an index() call, too.

I beg to differ, since a match is more intuitively readable (to a Perl
programmer) and AFAIK when a constant string is involved, then it is
optimized to a simple index() anyway.


Michele
 
B

Ben Morrow

Quoth Michele Dondi said:
I beg to differ, since a match is more intuitively readable (to a Perl
programmer) and AFAIK when a constant string is involved, then it is
optimized to a simple index() anyway.

It may actually be optimized further than that: if the match has a
fixed portion, and the matched string is shorter than that, the match is
not even attempted.

Ben
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top