lomSpace has asked for the wisdom of the Perl Monks concerning the following question:
use strict; #open file open(my $in,"/Users/mydir/Desktop/CCDS.current.txt") or die " Can't op +en file: $!"; #open out file open(OUT, ">/Users/mydir/Desktop/genesperchrcnt.txt"); # initialize the hash my %geneids=(); #open the file and push the info from the designated columns into it # remove header my $firstline = <$in>; chomp $firstline; while(<$in>){ chomp; # remove the newline character my @fields = split (/\t/); #extract the columns that we are interested in. # Populate the key value pairs of the hash with $gene and $id $geneids{$fields[2]} = $fields[0]; # initialize an array to store hash values my @chr; push @chr, $fields[0]; #count chromosome number which is the value in the hash $geneids{$fields[0]}++; next if $geneids{$fields[0]} > 1; foreach my $values (sort values %geneids) { print OUT "Chromosome $values has $geneids{$values} genes\n"; } } close($in); close(OUT); =cut Output Chromosome 1 has 1635 genes Chromosome 1 has 1635 genes Chromosome 1 has 1635 genes Chromosome 1 has 1635 genes Chromosome 3 has 778 genes Chromosome 3 has 778 genes Chromosome 3 has 778 genes Chromosome 3 has 778 genes Chromosome 4 has 518 genes Chromosome 4 has 518 genes Chromosome 4 has 518 genes Chromosome 4 has 518 genes
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how can I print my hash values once?
by ikegami (Patriarch) on Mar 10, 2011 at 05:16 UTC | |
by lomSpace (Scribe) on Mar 10, 2011 at 14:29 UTC | |
by ikegami (Patriarch) on Mar 10, 2011 at 19:55 UTC | |
by lomSpace (Scribe) on Mar 11, 2011 at 15:17 UTC | |
by ikegami (Patriarch) on Mar 11, 2011 at 21:50 UTC | |
by umasuresh (Hermit) on Mar 10, 2011 at 14:59 UTC | |
|
Re: how can I print my hash values once?
by roboticus (Chancellor) on Mar 10, 2011 at 13:52 UTC |