Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Reading Multiple files

by maheshkumar (Sexton)
on Aug 07, 2012 at 16:21 UTC ( #986025=note: print w/replies, xml ) Need Help??

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

I am not an expert in perl but the code that i have written already defines a file name so what should i do with that? Sorry I did not understand your code completely I am still doing it. Any help on this?

Replies are listed 'Best First'.
Re^3: Reading Multiple files
by 2teez (Priest) on Aug 07, 2012 at 18:13 UTC

    ..the code that i have written already defines a file name..

    The script I wrote, was on the premises that you want to write your selected "data", from each of the file, to a new file each.
    So, if your intention, then is to use just one file, to get all the selected "data", from various files in the directory ( or directories ), since you said you already have a filename. Then, you might have to modify, your code a bit.
    However, I might be wrong here. Please, if I may clear thing out.
    How are you getting your new files?
    Are you writing, all your output to a single file?
    Or to a new file for EACH file you are reading from?

      Basically what i am doing is that I have around 100 text files with different file names (random names) for example TR1, TR2 and so on. I am extracting data from TR1 and saving it in another file called TR1_Result. I want a code which does this on all files and creates new result file for each of them


        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://986025]
[Corion]: Heh - I just realized, $work management just declared "no raises for anybody this year", and I'm not even angry about that. Even though that implicitly means "raises for the people with an automatic 5% raise in their contract", which the newly-merged ...
[Corion]: ... coworkers have. But I guess I've gone more mellow since I get to relax more, and such stuff doesn't make me as angry as it used to.
[Corion]: $boss will still get to listen to my interpretation :-D
[Eily]: hey, I'm just behind Larry in SioB \o/

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-01-22 11:08 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.