in reply to How to group by a column and calculate max/min on another
If the modification is unclear, you can use Data::Dumper to output the resultant structure by adding the following to the end of your script:#!/usr/bin/perl -w use strict; use List::Util qw(max); use List::Util qw(min); #my $input0 = $ARGV[0]; #open (DATA,$input0) || die "cannot open input0"; my %gene_hash; while(<DATA>) { chomp; my ($chr, $start, $end, $gene, $ex) = split(/\t/, $_); my $gene_key = $chr.":".$gene; push( @{ $gene_hash{$gene_key}{start} }, $start ); push( @{ $gene_hash{$gene_key}{end} }, $end ); push( @{ $gene_hash{$gene_key}{ex} }, $ex ); } foreach my $key (keys %gene_hash) { my ($c, $g) = split(/\:/, $key ); print "$c\t$g\t"; my $Low=min( @ {$gene_hash{$key}{start} } ); my $High=max( @ {$gene_hash{$key}{end} } ); my $High_ex=max( @ {$gene_hash{$key}{ex} } ); { print "$Low\t$High\t$High_ex"; } print "\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
use Data::Dumper; print Dumper \%gene_hash;
A couple of minor things you may consider in addition:
|
|---|
| 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 14:06 UTC |