I'd do it something like this:

my @genes; # I assume these come from somewhere for my $style (qw/ g2d pocus dgp /) { my($in, $out) = ($style . "_ranking", $style . "_rankratio"); # step 1: locate and sort the relevant genes for this ranking styl +e my @set = sort { $a->{$in} <=> $b->{$in} } grep $_->{$in}, @genes; # step 2: get a count my $total = @set; # step 3: update the located genes with a rankratio for my $index (0 .. $#set) { $set[$index]{$out} = ($index + 1) / $total; } }

Notes: I select the genes that have a particular ranking specified by checking $_->{$in} in a boolean context, so that it'll be false for zero or undef without warnings; I never store the intermediate re-based ranking as it is implicit in the order of @set; I only loop over the selected genes to update them, so I won't try to update an undef value, nor (since there would be zero loop iterations) will this try to divide by zero.

This code is untested.

Hugo


In reply to Re: converting list of rankings into list of rankratio's. by hv
in thread converting list of rankings into list of rankratio's. by BioGeek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.