in reply to open input files once
If you don't want to read the files twice you need to think about your data structure and how you turn it into the desired output. Something like this should work, read the data into a temporary data structure with the file as a secondary key. Then process it to fill in the blanks at output time.
#!/usr/bin/perl use strict; use warnings; my @files = glob("file*.tab"); my %data; for my $file ( @files ) { open my $fh, '<', $file or die $!; while ( <$fh> ) { chomp; my @columns = split /\t/; die "_ERROR_ not 9 columns [ $_ ]\n" if @columns != 9; my $key = join( "\t", @columns[0..3] ); $data{$key}->{$file} = $columns[6]; } close $fh; } print join("\t", 'chr', 'fivep', 'threep', 'strand', @files), "\n"; for my $key ( keys %data ) { my ( $chr, $fivep, $threep, $strand ) = split /\t/, $key; next if ( $strand =~ /^0$/ ); my @output; for my $file ( @files ) { push @output, $data{$key}->{$file} // 0; } print join("\t", $chr, $fivep, $threep, "-", @output ), "\n"; }
I also cleaned up the code a little and added some whitespace around things as I find it makes it easier to read.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: open input files once
by Tux (Canon) on Jul 14, 2020 at 10:25 UTC | |
by rnewsham (Curate) on Jul 14, 2020 at 11:47 UTC | |
|
Re^2: open input files once
by v15 (Sexton) on Jul 14, 2020 at 18:10 UTC | |
by haukex (Archbishop) on Jul 14, 2020 at 20:11 UTC | |
by Tux (Canon) on Jul 14, 2020 at 18:58 UTC |