Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Opening multiple files for input

by meenac (Initiate)
on Dec 14, 2004 at 22:40 UTC ( [id://414881]=perlquestion: print w/replies, xml ) Need Help??

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


Hi All,
I need to read multiple files from a directory for
input and I am interested in all sec*.csv files. My
script now reads only one file.
$LOGDIR = 'C:\temp\logs'; open(INPUT, "$LOGDIR\\sec-ghost01-12-09-2004-21-43.csv") || die ("can +not open log file\n"); foreach my $line (<INPUT>) { chomp($line); process each line }

Also, the files are zipped. Is there a way I can
unzip it inside my perl script before reading? I am
running this script
on Windows 2003 using activeperl and I have a
command line gunzip utility.

Edited by davido to repair code tags.

Replies are listed 'Best First'.
Re: Opening multiple files for input
by William G. Davis (Friar) on Dec 14, 2004 at 23:32 UTC

    Try using Compress::Zlib to uncompress the compressed files and then Text::xSV to handle the comma-separated values.

    Also, in the future, please wrap all of your code inbetween opening and closing <code> </code> tags so it displays properly. You don't need to put <code> tags at the start of each line ;-)

Re: Opening multiple files for input
by sgifford (Prior) on Dec 14, 2004 at 23:48 UTC
    Part of what you want is a simple loop:
    foreach my $file ("$LOGDIR\\sec-ghost01-12-09-2004-21-43.csv", "$LOGDIR\\sec-ghost01-12-10-2004-21-43.csv", "$LOGDIR\\sec-ghost01-12-11-2004-21-43.csv") { open(INPUT,"< $file") or die "Can't open '$file': $!\n"; while (my $line = <INPUT>) { chomp($line); # process } }

    If you don't want to list the files explicitly, you'll want to use opendir and readdir, or else follow Zed_Lopez's suggestion and use glob.

Re: Opening multiple files for input
by Zed_Lopez (Chaplain) on Dec 14, 2004 at 23:35 UTC

    Also look up glob (perldoc -f glob) for a quick and easy way to find all your csv files to open them.

Re: Opening multiple files for input
by larryp (Deacon) on Dec 15, 2004 at 06:34 UTC

    Hi Meenac,

    The purpose of the opening/closing <code> tag pairs is to format your code in a readable format. You only need to surround your code with a single set of opening/closing <code> tags. For example, in the code you posted, notice how you're including several code tags, but you've forgotten the closing code tags and as such you're missing the formatting. If you surround all your code with a single set of tags, it will look like this:

    $LOGDIR = 'C:\temp\logs'; open(INPUT, "$LOGDIR\\sec-ghost01-12-09-2004-21-43.csv") || die ("cann +ot open log file\n"); foreach my $line (<INPUT>) { chomp($line); process each line }

    Check out these resources for more information on formatting your posts:

    Those links will help you get the answers you seek, and make life a whole lot easier for the people who are helping you. :)

    /Larry

    Update: davido cleaned up the errant code tags, so you won't be able to see them in your original post. However, you should still check out those links for future posts. :)

Re: Opening multiple files for input
by rev_1318 (Chaplain) on Dec 15, 2004 at 15:11 UTC
    The following script processes all .csv file in the directory as one stream:
    #!/usr/bin/perl use strict; use warnings; my $logdir = 'c:/tmp/logs'; { opendir DIR, $logdir or die "error opening $logdir: $!\n"; local @ARGV = map { $logdir . '/' . $_ } grep /\.csv$/, readdir DI +R; closedir DIR or die "error closing $logdir: $!\n"; while ( <> ) { #process the line } }
    HTH,
    Paul

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-28 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found