From the first part of your code it looks like you are storing your variables and values in the %data hash in the form $data{"variable name"} = "value". In order to work, your formulae have to use those variables and values in the very same format.

I would think it be useful to see a couple of examples.

Update: Here is some basic code to get you started:

use strict; use warnings; my %data = ( x => 5, y => 7, z => 2 ); my @formulae = ( 'x + y', 'z + 3', 'x ** z', 'a + x', 'x /= y', 'x - x' ); for my $formula (@formulae) { print "Processing formula $formula\n\t"; my @error = (); while ($formula =~ s|([a-z]+)|$data{$1}//' '|e) { push @error, $1 unless exists $data{$1}; } if (@error) { print "Error: unknown variable(s) @error\n\n"; } else { my $result = eval($formula); if( defined $result) { print "Result: $formula = $result\n\n"; } else { print "Syntax error in $formula\n\n"; } } }

In reply to Re: How to evaluate a mathematical formula that is stored in another file? by hdb
in thread How to evaluate a mathematical formula that is stored in another file? by skooma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.