As almut said, as the size of the file grows, so will the hashes. I came up with a solution that only keeps one query in a hash at a time. Then your entries won't accumulate in the hash.

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

#!/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
Output
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.


In reply to Re: Parsing and Finding Max Min by Cristoforo
in thread Parsing and Finding Max Min by Perl_Crazy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.