N
nancy.iida
I am trying to parse strings containing both numbers and operator
characters into an integer. I'm having some problems doing this.
Here's my program:
#!/usr/local/bin/perl
$number = 15 + 0.5 * 30; # this expression evaluates correctly to 30
$d2 = "15+0.5*30"; # this is the string I want to evaluate
$intd2 = int $d2; # will int create the value I want?
$floatd2 = sprintf "%f",$d2; # will sprintf to a float give me what I
want?
printf "number = $number\n";
printf "d2 = $d2\n"
printf "intd2 = $intd2\n"
printf "floatd2 = $floatd2\n";
Here's the resultant output:
number = 30
d2 = 15+0.5*30
intd2 = 15
floatd2 = 15
I was hoping to get the operators evaluated in the string expression?
Is there anyway of doing this short of parsing out the operators and
setting up the math? Am I just doing something wrong here?
Any help appreciated.
characters into an integer. I'm having some problems doing this.
Here's my program:
#!/usr/local/bin/perl
$number = 15 + 0.5 * 30; # this expression evaluates correctly to 30
$d2 = "15+0.5*30"; # this is the string I want to evaluate
$intd2 = int $d2; # will int create the value I want?
$floatd2 = sprintf "%f",$d2; # will sprintf to a float give me what I
want?
printf "number = $number\n";
printf "d2 = $d2\n"
printf "intd2 = $intd2\n"
printf "floatd2 = $floatd2\n";
Here's the resultant output:
number = 30
d2 = 15+0.5*30
intd2 = 15
floatd2 = 15
I was hoping to get the operators evaluated in the string expression?
Is there anyway of doing this short of parsing out the operators and
setting up the math? Am I just doing something wrong here?
Any help appreciated.