in reply to Re: Converting percentage into number
in thread Converting percentage into number

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... ;-)