I wonder how you generate your list of files.

Using the glob function enables you to use a finename mask giving you only the files matching this mask. This makes thing easier For example:

my @file_list = glob "./mypath/CHECK*.txt";

This filters out all filenames that do not start with CHECK and end with .txt. Note that, in this case, the "CHECK*.txt" string is not a Perl regex, it is an operating system file matching pattern (meaning inter allia that, here, the * stands for any group of characters and that you don't have to escape the dot). The glob function has the additional advantage that it will return you a list of files with their relative path, which is usually handy if you need to fo further work on these files.

This glob function is quite practical, but this will not solve the issue of the files that you want to exclude, but you are not stating clearly the rules for exclusion (just one example is definitely not a spec). Assuming for the sake of example that having a second underscore in the name is a reason for exlusion, you could do this in one go as follows:

my @file_list = grep {! /_.*_/} glob "./mypath/CHECK*.txt";

This single line above replaces the opendir and readdir statements and the regexes on the file names. (The regex in the grep block is not particularly efficient and can be improved if needed but it is just given as a very simple example.)


In reply to Re: how to check whether a particular word exists in filename by Laurent_R
in thread how to check whether a particular word exists in filename by learner@perl

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.