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

Re: segregating data from one file to many files

by jwkrahn (Abbot)
on May 08, 2009 at 19:22 UTC ( [id://762916]=note: print w/replies, xml ) Need Help??


in reply to segregating data from one file to many files

You can just open all the output files first.   Something like this:

#!/usr/bin/perl use strict; use warnings; my %files; for my $i ( '01' .. '15' ) { open my $OUT, '>', "output_$i.txt" or die "can not create 'output_ +$i.txt' $!\n"; my $pattern = sprintf 'S%05dGM', $i; $files{ $pattern } = { fh => $OUT, count => 0 }; } open my $FH, '<', 'input.txt' or die "can not open 'input.txt' $!\n"; while ( my $line = <$FH> ) { my $four = ( split /\t/, $line )[ 3 ]; my $key = substr $four, 0, 8; if ( exists $files{ $key } ) { print { $files{ $key }{ fh } } $line; $files{ $key }{ count }++; } } close $FH; for my $key ( keys %files ) { unless ( $files{ $key }{ count } ) { print { $files{ $key }{ fh } } "NO HITS\n"; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 22:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found