Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Processing multiple files

by jwkrahn (Monsignor)
on Dec 12, 2012 at 19:56 UTC ( #1008578=note: print w/ replies, xml ) Need Help??


in reply to Processing multiple files

what would you do differently
#!/usr/bin/perl use warnings; use strict; #test file to learn how to process multiple files at once #first get the path for current directory my $dir = '.'; # current working directory #open and read only the .txt files opendir my $DIR, $dir or die "Cannot open '$dir' because: $!"; my @textFiles = grep /\.txt\z/, readdir $DIR; closedir $DIR or die "Could not close '$dir' because: $!"; #open each file for further processing foreach my $text_file ( @textFiles ) { open my $text_file_fh_in, '<', $text_file or die "Cannot open '$te +xt_file' because: $!"; #open for reading #create the names of the new files my $text_file_out = $text_file; $text_file_out =~ s/(?=\.txt\z)/_new/; open my $text_file_fh_out, '>', $text_file_out or die "Cannot open + '$text_file_out' because: $!"; #open for writing #tests to see if it works #print "$text_file\n"; print "$text_file_fh_in\n"; #print "$text_file_out\n"; print "$text_file_fh_out\n"; while ( <$text_file_fh_in> ) { if ( /^(.*?)\.(.*?)\ (.*?)\.(.*?)\ (.*?)$/ ) { print $text_file_fh_out "$1\t$2\t$4\t$5\n"; } } #close the filehandles close $text_file_fh_in or die "Could not close '$text_file' becaus +e: $!"; close $text_file_fh_out or die "Could not close '$text_file_out' b +ecause: $!"; }


Comment on Re: Processing multiple files
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-31 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls