Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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: $!"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008578]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2018-06-18 16:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.