Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Directory Listing

by skyler (Beadle)
on Jun 26, 2003 at 00:01 UTC ( #269058=perlquestion: print w/replies, xml ) Need Help??

skyler has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I need to write a program that reads a file and grab a value from a string then it matches the sting against a list of files in a directory. The files are formatted (mj123456.pdf, etc) the string that I'm trying to match is "123456" I know how to read files and grab strings but I have not dealt with matching a string against a list of files in a directory. sort of like a search base on a pattern. I will appreciate any suggestions on how to write the program.

Replies are listed 'Best First'.
Re: Directory Listing
by Zaxo (Archbishop) on Jun 26, 2003 at 00:10 UTC

    The expressions glob('*123456*') or else grep {/123456/} readdir DIR will return a list of matching file names.

    After Compline,

      Just a quick note to say if there's supposed to be only one matching file, might have to be careful about the matching.

      Zaxo's code will find any files which have that number string in it.

      But if there are two or more, like if you have "ab123456.pdf" and "ab1234567.pdf" and "ab9123456.pdf" then you need a more detailed regex.

      “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
      M-J D
Re: Directory Listing
by crouchingpenguin (Priest) on Jun 26, 2003 at 00:43 UTC

    See also Searching file extensions.

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://269058]
Approved by grep
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2021-10-19 06:43 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (76 votes). Check out past polls.