Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Searching a whole directory of databases

by drewboy (Sexton)
on Jun 23, 2002 at 09:50 UTC ( #176578=perlquestion: print w/replies, xml ) Need Help??

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

I have one search script (written in perl) that's designed to search only one database:

open (DB, "<database.db")

However, now I'm spidering websites and assigns one database for each site (all in .txt format) (i.e. cnn.txt, aol.txt, mtv.txt, yahoo.txt) All files are saved in a folder called "searchdbs". I wonder if it would be possible for my script to just look into the searchdbs folder and open each database inside of it rather than tweaking my script each time I spider a new website. Looking forward to your help... Thanks as always...:-)

Replies are listed 'Best First'.
Re: Searching a whole directory of databases
by crazyinsomniac (Prior) on Jun 23, 2002 at 10:15 UTC
Re: Searching a whole directory of databases
by mfriedman (Monk) on Jun 23, 2002 at 10:05 UTC
Re: Searching a whole directory of databases
by pjf (Curate) on Jun 24, 2002 at 03:01 UTC
    You might find the following skeleton useful. This assumes you're already in the searchdbs folder.
    foreach my $file (<*.txt>) { open(FILE,"< $file") or die "Cannot open $file - $!\n"; # Read in from FILE and do stuff with it... }
    Of course, There's More Than One Way To Do It. You might find it easier dropping all the filenames in @ARGV and then using Perl's magic diamond operator:
    @ARGV = <*.txt>; while (<>) { # Files will now be opened and read automagically, and # each line placed in $_. You can see which file # you're currently reading from by looking at $ARGV. }
    Understanding how the above works is left as an exercise for the reader. ;)

    All the best,
    Paul Fenwick
    Perl Training Australia

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2023-01-28 16:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found