Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Converting percentage into number

by l.frankline (Hermit)
on Jan 10, 2007 at 11:53 UTC ( [id://593885]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://593885]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-25 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found