If you're going to worry about with and without decimal places (++ for that), try:
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:#!/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
It will leave the corruption alone this way. Detecting the corruption is as simple as checking if there are remaining %'s after the substitution:s|\b($RE{num}{decimal})%|$1 * 0.01|eg;
Ok, maybe I'm going beyond the original post's scope here... ;-)if (/%/) { print STDERR "Input has been corrupted!"; # die ? }
In reply to Re^2: Converting percentage into number
by Tanktalus
in thread Converting percentage into number
by uv2007
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |