in reply to Help with loops
This code takes advantage of the fact that the "match" operator (m//) provides an array of every match when used with the /g modifier. Doing it this way lets us dispense with all those messy splits. The "eval" command tells Perl to actually "do the math" with the resulting fractions, printing (for example) "0.5" rather than just the string "1/2".while (<DATA>) { @fractions = m!(\d+/\d+)!g; print eval($_), " " foreach @fractions; print "\n"; } __DATA__ SYMM 1/8 - X, 1/8 + Y, 1/8 + Z SYMM 1/6 - X, 1/6 + Y, 1/6 + Z SYMM 1/4 - X, 1/4 + Y, 1/4 + Z SYMM 1/2 - X, 1/2 + Y, 1/2 + Z
Your next question might be on how round the resulting fractions accordingly. For that, I point you to perfunc and the sprintf function.
Enjoy!
Gary Blackburn
Trained Killer
|
|---|