Re: math formula substitution and evaluation



Daniel D Jones wrote:
> Given something like the following:
>
> my @variables = [3, 7, 13, 4, 12];

You want round brackets here. You've created an array with just one element, with a reference to an anonymous array as its value.

> my @tests = ("2*a+b==c", "c-d+a==e");
>
> I need to be able to evaluate the mathematical truth of the tests, using the
> values from @variables, where $variable[0] holds the value of the
> variable 'a', $variables[1] holds the value of 'b', etc. I can do the
> evaluation but how do I (reasonably efficiently) substitute the values into
> the strings? (The length of the array and the exact tests are not known
> until run time.)
>
> In C, I'd do this by walking through the test strings character by character,
> checking for isalpha, converting the character to its ascii value and
> subtracting the ascii value of 'a', and indexing into the array. In Perl I'm
> not sure how to do this type of low level character by character processing.
> The only thing which occurs to me is to split() the string into an array of
> characters, then walk through that array. Am I on the right track or is
> there an easier way to do this in Perl?

Hi Daniel.

You can do exactly that in Perl, and a lot more simply:

my @variables = (3, 7, 13, 4, 12);
my @tests = ("2*a+b==c", "c-d+a==e");

foreach (@tests) {
s/([a-z])/$variables[ord($1) - ord('a')]/ge;
print $_, "\n";
}

OUTPUT

2*3+7==13
13-4+3==12

HTH,

Rob
.



Relevant Pages

  • Re: (*) and List Directed I/O
    ... character n of a character string could be accessed as b, ... of strings. ... || reference unless foo is declared as an array. ... || is a function reference. ...
    (comp.lang.fortran)
  • Re: Concatenate integer to string
    ... F2003 adds allocatable-length strings. ... Since a character with allocatable ... It seems that the deferred length has a special proparty, different from a deferred array size, in that the standard assignment operator will allocate the length. ...
    (comp.lang.fortran)
  • Re: Array range assignments and characters
    ... This follows the rules of assigning a scalar to an array. ... character t3*8 ... as a single character string/array has been used to contain 2 strings ... directly like Fortran character strings. ...
    (comp.lang.fortran)
  • Re: (*) and List Directed I/O
    ... >>> character n of a character string could be accessed as b, ... The ambiguity is that stringis assumed to ... > reference unless foo is declared as an array. ... decided to consider strings to be scalars and the presence ...
    (comp.lang.fortran)
  • Re: math formula substitution and evaluation
    ... (Brackets return a reference to an anonymous array containing the list, ... converting the character to its ascii value and ... foreach my $test { ... If the tests really do look like what you've displayed, then the easiest way would be to make sure your tests are proper perl code, and then run them through "eval". ...
    (perl.beginners)