Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: regex expression help

by ikegami (Patriarch)
on Nov 08, 2005 at 19:36 UTC ( [id://506855]=note: print w/replies, xml ) Need Help??


in reply to regex expression help

For the input you gave, it won't make a difference, but if $Y is a constant string, not a regexp, you should escape it, as follows:

foreach $Y (@dependantFiles) { foreach $X (@namedFiles) { if( $X =~ m/\Q$Y\E/i) { print "Found it\n" last; } } }

I also removed the useless 'g' option and the useless 'next'.

But if it's not a regexp, why use m// at all! index is much faster:

foreach $Y (@dependantFiles) { foreach $X (@namedFiles) { if (index(lc($X), lc($Y)) >= 0) { print "Found it\n" last; } } }

With a slight optimization:

foreach $Y (@dependantFiles) { my $lcY = lc($Y); foreach $X (@namedFiles) { if (index(lc($X), $lcY) >= 0) { print "Found it\n" last; } } }

If the $Y will always be at the end of $X, the following is even faster and more accurate:

foreach $Y (@dependantFiles) { my $lcY = lc($Y); foreach $X (@namedFiles) { if (lc(substr($X, -length($Y))) eq $lcY) { print "Found it\n" last; } } }

I haven't solved the problem where $X is c:\path\ab.exe and $Y is b.exe. There are a couple of solutions, but I'll let you try to solve it before I say more.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://506855]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-19 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found