Since you're using Windows I recommend you use Notepad++ or a similar programmers editor since it will help with closing brackets and parenthesis.

Also, you are likely to run into problems comparing strings with eq since filenames can have upper and lower case. Either use matching with the case insensitive modifier '//i' or 'm//i', or use the lower case function, lc(), before the compare.

use strict; use warnings; my @files = ( 'file1.txt', 'a.exe', 'my.log'); my @files2 = <DATA>; foreach(@files){ my $regex=$_; $regex =~ s/\./\\\./; # replace the '.' in file.ext # with '\.' so it will work in the match. print "\n\tuse $regex as the regex\n"; foreach my $file2(@files2){ chomp $file2; print "---", $file2, "\n"; if ($file2 =~ /$regex/i){ print "\tmatched with //i...\n"; if ($file2 ne $_){ print "\tbut not with string compare.\n"; } else {print "\tand with string compare.\n";} } } } #this one was missing __DATA__ File1.txt A.exe B.ext my.log

updated; fixed some goofy bugs


In reply to Re: Need help with comparison code by Gulliver
in thread Need help with comparison code by sowais

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.