in reply to How to group by a column and calculate max/min on another

If you structure your data a little differently, it will be easier to extract what you need:
use strict; use warnings; use List::Util qw(max min); my %gene_hash; while (<DATA>) { chomp; my ($chr, $start, $end, $gene, $ex) = split /\t/; push @{ $gene_hash{$chr}{$gene}{start} }, $start; push @{ $gene_hash{$chr}{$gene}{end } }, $end; push @{ $gene_hash{$chr}{$gene}{ex } }, $ex; } for my $chr (keys %gene_hash) { for my $gene (keys %{ $gene_hash{$chr} }) { my $Low = min( @ { $gene_hash{$chr}{$gene}{start} } ); my $High = max( @ { $gene_hash{$chr}{$gene}{end } } ); my $High_ex = max( @ { $gene_hash{$chr}{$gene}{ex } } ); print "$chr $Low $High $gene $High_ex\n"; } } __DATA__ chrX 2680092 2744539 XG 1 chrX 2680090 2744529 XG 2 chrX 2680080 2744519 XG 3 chrX 2680070 2744509 XG 4 chrX 2680070 2744509 DT 1 chrX 2680090 2744519 DT 2

prints out:

chrX 2680070 2744539 XG 4 chrX 2680070 2744519 DT 2

Replies are listed 'Best First'.
Re^2: How to group by a column and calculate max/min on another
by perl_paduan (Initiate) on Aug 03, 2010 at 13:57 UTC
    Thank you toolic! It works perfectly!

    Cheers,

    perl_paduan