CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
reading Multiple Files

 



perlkid
stranger

Aug 20, 2000, 7:59 PM

Post #1 of 7 (1241 views)
reading Multiple Files Can't Post

 
How can I read all of the files in a directory and treat the data as if it's in one array so I can search it?

Thanks,

perlkid


japhy
Enthusiast

Aug 20, 2000, 9:33 PM

Post #2 of 7 (1241 views)
Re: reading Multiple Files [In reply to] Can't Post

You might want to try using the magic of <> and @ARGV:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


{
local @ARGV = glob "*.txt";
@lines = grep /^foo/, <>;
# grab all the lines of all .txt files
# in this directory that start with 'foo'
}
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



perlkid
stranger

Aug 21, 2000, 3:40 PM

Post #3 of 7 (1241 views)
Re: reading Multiple Files [In reply to] Can't Post

 
Thank You Japhy,

But how do I point this code to the directory I want it to read the files of.

The file in the directory are all .db files so I don't have to worry about picking out certain files.

Thanks A Lot Japhy,

perlkid


Kanji
User

Aug 21, 2000, 3:54 PM

Post #4 of 7 (1241 views)
Re: reading Multiple Files [In reply to] Can't Post

perldoc -f opendir

... and I'd have to disagree with you about picking out files: . and .. are (on many systems) part of the directory, and are just two examples.

Who knows what else might turn up in your directories without you knowing?


japhy
Enthusiast

Aug 21, 2000, 4:15 PM

Post #5 of 7 (1241 views)
Re: reading Multiple Files [In reply to] Can't Post

perlkid, I'd like you to look at my example again, and look at the glob() function. glob() does a shell-like file glob. Doing glob("*.txt") would get all .txt files IN THE CURRENT DIRECTORY. So if you want to get them from a different directory, give the directory path.

Programming is about finding patterns. You'll be a far better programmer if you can find patterns.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



perlkid
stranger

Aug 21, 2000, 5:13 PM

Post #6 of 7 (1241 views)
Re: reading Multiple Files [In reply to] Can't Post

 Ok, Thanks Japhy,

But I tried doing that well before my previous post. I can't get the contents of all the files to be stored into an array I've tried everything I can think of with your code and I can't get it to work for me.

Kanji,

I know how to use -f opendir but I don't need that for what I'm trying to do.
I want to count the lines of all of the files in a directory. The number of files is always changing and there are too may to do one by one. There are about 1,500 files on average in each directory, and that number is always going up. Yet I need to count the combined total of all the lines in all the files. I also would like to be able to filter the files too. That is why I need to treat all of the files as one by storing them into an array.

It seems like japhy's code would work fine but I just don't get it??

Japhy, how would you then handle the data, wouldn't you handle the data through the array @lines?

I asumed that's what I would do since it seemed logical. I thought that <> was the data that was globbed from all of the .txt files. So I tried to print @lines to test it and I didn't get anything.

Can You still help me?

Thanks Guys,

But I see what You mean about picking out files kanji, Smile

Perlkid

the latest code I tried is

@all = glob("/dir/data/*.db");
print "@all";

No 500 error but nothing prints.

Probably way off anyway.

[This message has been edited by perlkid (edited 08-21-2000).]


perlkid
stranger

Aug 21, 2000, 5:22 PM

Post #7 of 7 (1241 views)
Re: reading Multiple Files [In reply to] Can't Post

 
Japhy!!!!!!!!!!

I'm sorry for questioning your Genius!!! Smile Smile Smile

It works GREAT!!

I made the dumbest mistake. Frown

Permissions, he he, oops sorry.

I never had a problem with 773 permissions before. Thanks a lot.

Smile Smile Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives