in reply to Converting percentage into number

Hi,

Hope this code will help you:

while (<DATA>) { if ($_=~m#\s+(\d+\.\d+)\%(.+)$#) { print $` . " " . $1 / 100 . $2 . "\n"; } } __DATA__ ENSP00000233379 1058 30 1206 1298 96.1% 13 + 96524483 96533474 8992 ENSP00000233379 1058 30 1206 1298 64.3% 13 + 96524483 96533474 8992 ENSP00000233379 1058 30 1206 1298 23.2% 13 + 96524483 96533474 8992

Output:

ENSP00000233379 1058 30 1206 1298 0.961 13 + 96524483 96533474 8992
ENSP00000233379 1058 30 1206 1298 0.643 13 + 96524483 96533474 8992
ENSP00000233379 1058 30 1206 1298 0.232 13 + 96524483 96533474 8992

regards,
Franklin

Don't put off till tomorrow, what you can do today.

Replies are listed 'Best First'.
Re^2: Converting percentage into number
by davorg (Chancellor) on Jan 10, 2007 at 12:02 UTC

    Simplifying slightly:

    while (<DATA>) { if (/(\d+\.\d+)%/) { print $` . $1 / 100 . $'; } }

    But I still prefer my s/// solution as using $` and its friends isn't recommended.

    The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches.
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg