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: Advanced:
Read multiple files from a directory

 



sohail
New User

Dec 2, 2003, 12:14 PM

Post #1 of 4 (1252 views)
Read multiple files from a directory Can't Post

Hi Everyone,

I have serveral files in a directory, each files has a numerical field. I wan't to read all the files and get an average of the nuerical field. See following example:
file1
xyz 125
abc 235
file2
xyz 145
abc 105
file3
xyz 135
abc 200

I want a new file with :
xyz (avg)
abc (avg)

Thanks


davorg
Thaumaturge / Moderator

Dec 3, 2003, 2:09 AM

Post #2 of 4 (1246 views)
Re: [sohail] Read multiple files from a directory [In reply to] Can't Post

(you know this really isn't an advanced question)

You'd use "opendir" and "readdir" to get a list of the files, "open" to open the files, <HANDLE> to read the data from the file and probably "chomp" and "split" to process the data.

Why not show us what you've already got and explain where you are having problems.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


sohail
New User

Dec 3, 2003, 5:53 AM

Post #3 of 4 (1243 views)
Re: [davorg] Read multiple files from a directory [In reply to] Can't Post

Actually, I want to know how to go through each file, store value for the numeric field, calculate avg. I know how to opendir,readir, and open filehandle...


davorg
Thaumaturge / Moderator

Dec 3, 2003, 6:43 AM

Post #4 of 4 (1242 views)
Re: [sohail] Read multiple files from a directory [In reply to] Can't Post

Have a hash where the keys are 'xyz', 'abc', etc. Use those to store a running total of the values. You'll also need another variable to store the number of files you have processed (so you can calculate the average).

Like I said before - show us the code. That'll make it far easier to help you.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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