Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Error in Code: Need help

by Util (Priest)
on Mar 28, 2012 at 23:27 UTC ( #962279=note: print w/replies, xml ) Need Help??

in reply to Error in Code: Need help

You can also make your code more robust by using the existence of the gene in the has instead of keeping "old" copied of the variables. This will allow your code to do-the-right-thing even when genes are in non-contiguous lines.

++tangent, who I see beat me to it.

#!perl use strict; use warnings; use autodie; my $out_path = 'output.txt'; my @lines = <>; chomp @lines; my %gene_maxfreq; for (@lines) { my ( $freq, $gene ) = split; my $max_freq = $gene_maxfreq{$gene}; if ( (!defined $max_freq) or $max_freq < $freq ) { $gene_maxfreq{$gene} = $freq; } } open my $out_fh, '>', $out_path; for (@lines) { my ( $freq, $gene ) = split; my $max_freq = $gene_maxfreq{$gene}; if ( defined $max_freq ) { my $line = join "\t", $freq, $gene, $max_freq; print {$out_fh} "$line\n"; print "$line\n"; } } close $out_fh;

Replies are listed 'Best First'.
Re^2: Error in Code: Need help
by angerusso (Novice) on Mar 29, 2012 at 00:24 UTC

    THANKS SO MUCH for your another SUPERCOOL code and help. Very much appreciated.!!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://962279]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2021-10-19 18:20 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (77 votes). Check out past polls.