Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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,
    Zaxo

      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.


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2021-10-18 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (74 votes). Check out past polls.

    Notices?