Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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 chilling in the Monastery: (8)
As of 2014-08-31 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls