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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.