Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: open input files once

by BillKSmith (Monsignor)
on Jul 14, 2020 at 19:25 UTC ( [id://11119318]=note: print w/replies, xml ) Need Help??


in reply to open input files once

The following approach is similar to your first pass. The entire output hash is built in this pass. Every time I see a new key, rather than initializing it with an empty array, I use an array of zeros and overwrite the one corresponding to the file. Every time I find an existing key, I overwrite the zero which corresponds to both the file and that key. The resulting hash of arrays is passed to the print section. It prints one line for each key. It corrects the format of srand then prints the corrected key and the values of the data array.
use strict; use warnings; use feature 'state'; my @files = glob "file*.tab"; my %output; foreach my $file (@files) { open my $fh, '<', $file or die $!; state $fn = -1; $fn++; while (<$fh>) { chomp; my @s = split /\t/; die "_ERROR_ not 9 colmns [$-]\n" if @s != 9; my $key = join "\t", @s[0..3]; $output{$key} = [('0') x @files] if (!exists $output{$key}); $output{$key}[$fn] = $s[6]; } close $fh; } print join("\t",'chr','fivep','threep','strand',@files),"\n"; foreach my $key (sort keys %output) { my $values = $output{$key}; my $temp_key = $key; $temp_key =~ s/[^\t]+$/ ($& eq 0) ? '+' : '-'/e; print join( "\t", $temp_key, @$values ), "\n"; }
Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-19 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found