There are two functions you can use to get the contents of a directory. glob and readdir. glob is the slowest, but allows you to limit the types of files you get back. eg
my @files = glob("*_hm1_111");
will return you all files ending with hm1_111, but you need more flexibility than this, so readdir is probably the best choice.

readdir on the other hand is very fast, and returns you the files in whatever order they are stored in the internal representation for your file system. This will not always be alpha/ascii-betical. glob always returns files sorted ascii-betically.

The last difference between the two is how . (dot) files are handled. glob("*") will not return files such as .bashrc but readdir will. glob(".*") must be called if you want the . files. (In fact glob works very much like the UNIX c-shell.)

Having said all of this, I'd suggest you use ovid's suggestion in most circumstances. However, if it is likely that your directory contents will be very large (and you are only interested in a small fraction of the files) and if performance is important then I'd suggest you compare ovid's suggestion with something like this:

my @files = grep /^(?:ap|ck)_hm1_111/, glob("DIR/*_hm1_111");
and go with the best.

Jacinta


In reply to Re: Reading Filenames by jarich
in thread Reading Filenames by jakeeboy

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.