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

Re: Find highest value

by choroba (Abbot)
on Sep 19, 2012 at 14:07 UTC ( #994460=note: print w/ replies, xml ) Need Help??


in reply to Find highest value

Naming variables c1 .. c10 suggests you need an array.

To find a maximum, you can use List::Util.

The following code returns "$chr $begin $end $class" if the class has the maximal total, or 'undefined' instead of the class number if there are more classes having the same maximum. Tweak it to serve your needs:

#!/usr/bin/perl use warnings; use strict; use List::Util qw/max/; <>; # skip header for my $line (<>){ my ($chr, $nr, $begin, $end, @values) = split ' ', $line; my %total; while (@values) { my $class = shift @values; my $total = shift @values; $total{$class} = $total; } my $max = max values %total; my @maxes = grep $total{$_} == $max, keys %total; if (@maxes == 1) { print "$chr $begin $end $maxes[0]\n"; } else { print "$chr $begin $end undefined\n"; } }
Updated code.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Find highest value
Download Code
Re^2: Find highest value
by linseyr (Acolyte) on Sep 19, 2012 at 14:34 UTC
    Thank you for your answer. But the last columns are classes and totals of classes, so I only want to find the maximum of the totals of classes. I should select only subset of the @values and put in array then, but this doesn't work:
    push(@totals, values[1], values[3], values[5], values[7]);
    Is there a way to do this? I want to find the class for which the total of class is the highest, and print the class name..

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-08-21 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (141 votes), past polls