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.


In reply to Re: regex expression help by ikegami
in thread regex expression help by JFarr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.