in reply to Converting percentage into number

Assuming that your percent-values are always preceded by whitespace, and is always a valid numeric, the following should cope with values with and without decimal places:

s/(\S+)%/$1*0.01/eg #e = evaluate right-side as expression, g = global +, i.e. change all percent-values found, not just the first
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm@tomandlu.co.uk

Replies are listed 'Best First'.
Re^2: Converting percentage into number
by Tanktalus (Canon) on Jan 10, 2007 at 15:37 UTC

    If you're going to worry about with and without decimal places (++ for that), try:

    #!/usr/bin/perl use strict; use warnings; use Regexp::Common; while (<DATA>) { s|($RE{num}{decimal})%|$1 * 0.01|eg; print; } __DATA__ ENSP00000233379 1058 30 1206 1298 96.1% 13 + 96524483 96533474 8992 ENSP00000233379 1058 30 1206 1298 96% 13 + 96524483 96533474 8992
    Ok, so you have to go install Regexp::Common. Getting this right every time is well worth that minor expense, IMO. Otherwise, if somehow the data gets corrupted so there is, say, "S%" in there somewhere, your code will generate an error. To take care of "S6%", just add a \b:
    s|\b($RE{num}{decimal})%|$1 * 0.01|eg;
    It will leave the corruption alone this way. Detecting the corruption is as simple as checking if there are remaining %'s after the substitution:
    if (/%/) { print STDERR "Input has been corrupted!"; # die ? }
    Ok, maybe I'm going beyond the original post's scope here... ;-)