in reply to Parsing and Finding Max Min
foreach $Query ( keys (%hash2) )
It isn't necessary to loop over the keys in hash2. Just use the first loop and, in your print string, $hash2{$Query}[2] will display ok.
As others have noted, it's not clear why you want to re-assign all the values in the field when in a condition where you have a new max or min.
In your sample data, the only fields that changed were the start, end fields.
Chris
Output#!/usr/bin/perl use strict; use warnings; my @cols = qw/ score start stop one two /; my %line_before; my ($query, @data) = split /\t/, <DATA>; chomp $data[-1]; # last @data has a newline @{ $line_before{$query} }{ @cols } = @data; my ($min, $max) = @data[1, 2]; while (<DATA>) { chomp; my %current_line; ($query, @data) = split /\t/; @{ $current_line{$query} }{ @cols } = @data; # if the prior and current queries don't match if (! exists $line_before{ $query }) { print_record(\%line_before, \@cols); ($min, $max) = @{ $current_line{$query} }{ qw/ start st +op / }; } else { my $start = $current_line{ $query }{ start }; if ($start < $min) { $min = $start; } else { $current_line{ $query }{ start } = $min; } my $stop = $current_line{ $query }{ stop }; if ($stop > $max) { $max = $stop; } else { $current_line{ $query }{ stop } = $max; } } %line_before = %current_line; print_record(\%line_before, \@cols) if eof; } sub print_record { my ($rec, $cols) = @_; my ($query) = keys %$rec; # There is only 1 key print join("\t", $query, @{ $rec->{$query} }{ @$cols }), "\n"; } __DATA__ Books 6 159290954 159291342 + Author Books 6 159294558 159294653 + Author Books 6 159316253 159316398 + Author Books 6 159330999 159331385 + Author Books 6 159290971 159290997 + Author Books 6 159316253 159316398 + Author Books 6 159330999 159331289 + Author Books 6 159316268 159316398 + Author Books 6 159330999 159331245 + Author Coopy 1 123456789 987654321 + Author
C:\perlp>perl 814937.pl Books 6 159290954 159331385 + Author Coopy 1 123456789 987654321 + Author
Update: Captured 'query' in $query (and changes to array indices). Reset min - max when a record changed.
Update 2: I was 'assuming' the queries came in groups (that query only to appear in that bunch). Is that the case? Or, can they be interspersed.
|
|---|