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

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
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-11-28 21:56 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls