in reply to Arithmetic with fractions
You need to evaluate the string "6/2" to get the numerical value 3 before you try to do the addition. You can do it using eval but eval on strings is dangerous as it will *execute* the code in the string. This function should be safe to use as it will only do the eval if the string contains + - . 0 1 2 3 4 5 6 7 8 9 / plus spaces and tabs which are harmless. These chars should be all you need for the task at hand....
sub frac2dec { my $str = shift; $str = eval $str if $str =~ m!^[\-\+\d\./ \t]+$!; $str; } $a = " 6/2"; $b = " 4 / 8 "; print frac2dec($a) + frac2dec($b); __DATA__ 3.5
cheers
tachyon
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Arithmetic with fractions
by calin (Deacon) on Sep 22, 2004 at 14:34 UTC | |
by ikegami (Patriarch) on Sep 22, 2004 at 15:41 UTC | |
by calin (Deacon) on Sep 22, 2004 at 17:51 UTC | |
|
Re^2: Arithmetic with fractions
by algonquin (Sexton) on Sep 22, 2004 at 05:55 UTC | |
|
Re^2: Arithmetic with fractions
by algonquin (Sexton) on Sep 22, 2004 at 15:46 UTC |