48 users online (1 members and 47 guests)  


  Results 1 to 2 of 2

Related

  1. Convert asp to pdf    Forum: ASP Forum
    Replies: 0
  2. Help! ASP convert PDF    Forum: ASP Forum
    Replies: 5
  3. Convert date dd/mm/yyyy to ddd ?    Forum: PHP Forum
    Replies: 1
  4. Convert many images    Forum: Graphics Forum
    Replies: 2
  1. #1
    gee2's Avatar
    New User

    Status
    Offline
    Join Date
    Oct 2006
    Posts
    1

    convert math operators from string

    I want a script to take a simple equation, and calculate the answer, where the math operators are variable, e.g.,

    a + b * c = answer

    <snip>
    $a = 3;
    $b = 4;
    $c = 5;

    $sign1 = "+";
    $sign2 = "*";

    $answer = "$a $sign1 $b $sign2 $c";
    print $answer;
    <snip>

    But that prints the equation, i.e.,
    3 + 4 * 5
    not the numeric answer I want to the equation, i.e.,
    23

    Suggestions? TIA.

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: convert math operators from string

    you can use perl's eval function to do what you want...

    Code:
    $a = 3; 
    $b = 4; 
    $c = 5; 
    
    $sign1 = "+"; 
    $sign2 = "*"; 
    
    $answer = eval("$a $sign1 $b $sign2 $c"); 
    print $answer;
    try that and see if it works out,
    -ALL



Tags for this Thread