Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Reorganizing file contents

by rjt (Deacon)
on Jun 08, 2010 at 17:27 UTC ( #843710=note: print w/ replies, xml ) Need Help??


in reply to Reorganizing file contents

The following should be reasonably efficient. It does keep files open, one per unique DESCR element. Depending on how many DESCR elements you have in your real data, you may need to rethink this, possibly with a least-recently-used scheme.

use warnings; use strict; my %fh_of; # Hash of filehandles foreach my $file (<R*-*.txt>) { open INPUT, "<$file" or die "Couldn't open $file: $!"; while (<INPUT>) { my $fh; if (/^DESCRP\s+(.+?)$/) { my $des = $1; unless (exists $fh_of{$des}) { open $fh_of{$des}, ">>$des.txt" or die "Couldn't open $des.txt: $!"; } $fh = $fh_of{$des}; $file =~ /^(R.+?)-/; # Glob guarantees match print $fh "$1$des\n"; next; } print $fh $_ if $fh; } close INPUT; } close $_ for (values %fh_of);

With the input files as you've given them, I get the expected output. All errors are fatal; you might want to handle them more gracefully depending on your application. If an input file does not start with a DESCRP line, $fh will not be defined, so I just throw away records until I see a DESCRP line. Again, you may want to handle this differently.


Comment on Re: Reorganizing file contents
Download Code
Re^2: Reorganizing file contents
by tomdbs98 (Beadle) on Jun 08, 2010 at 18:21 UTC
    Worked beautifully, unfortunately I am out of votes. :P

    I should be able to touch up your solution just fine for the real deal.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-10-21 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls