Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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.

Replies are listed 'Best First'.
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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2017-04-24 21:29 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (447 votes). Check out past polls.