Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^5: Reading Multiple files

by 2teez (Priest)
on Aug 08, 2012 at 16:39 UTC ( #986321=note: print w/replies, xml ) Need Help??

in reply to Re^4: Reading Multiple files
in thread Reading Multiple files


That is what the code I gave is really doing.

sub work_n_save { ... ## whatever my $filename = $File::Find::name if !-d; ## get file name if not + directory $filename = basename($filename); ## get the file base na +me ## The line below assumes, files have extention $filename =~ s/\..+$//; ## remove the file exte +ntion open my $fh_new, '>', $filename . "_Result" or croak "can't open f +ile: $!"; open my $fh, '<', $_ or croak "can't open file: $!"; while ( defined( my $line = <$fh> ) ) { chomp $line; ## do whatever you want print {$fh_new} $line, $/; } }
Please, don't forget that the subroutine "work_n_save" is called as sub. reference in the find subroutine of the module File::Find, which transverse the whole of the directory ( or directories).
Also, note the way the new files are opened:
open my $fh_new, '>', $filename . "_Result" or croak "can't open file: + $!";
Hope this helps.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986321]
[Corion]: ;)
[Corion]: Hi tye btw ;)
[Corion]: I found plenty of not chatty enough logs with Perl too ... I'm slowly coming to appreciate Log::Log4perl resp. our homegrown alternative
[tye]: my experience with python so far is more like finding a moderately useful error message is a shocking surprise.
Corion disappears
[tye]: I get an error string but no reason and no context. Like die "Could not read file.\n" but with a stack trace where all of the useful context is stripped.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-09-21 20:15 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (252 votes). Check out past polls.