Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^2: Regarding the polynomial evaluation. Stuck at one part. please helpppp!!

by freekngeek (Acolyte)
 on Jan 22, 2013 at 14:43 UTC ( #1014681=note: print w/replies, xml ) Need Help??

You mean the input for the subroutine ? right ?

```\$\$lvlhash{\$level}{'POLY_BASED_EM_DC'}{'EM_POLY'}{11} = -0.137 + (0.004
+79 / (W/0.9)) + (2.5593 * (W/0.9 )
\$\$refhash{\$level}{'MINDESIGNWIDTH'} = 0.045
when I print this :
```print ("\$\$lvlhash{\$level}{'POLY_BASED_EM_DC'}{'EM_POLY'}{11},\$\$refhash
+{\$level}{'MINDESIGNWIDTH'},0 \n");
I get this output on the terminal:
```-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W/0.9 ) ,0.045,0
which means there's no problem in reading the values, but somehow it's not giving me what I really want to see :D

Replies are listed 'Best First'.
Re^3: Regarding the polynomial evaluation. Stuck at one part. please helpppp!!
by tmharish (Friar) on Jan 22, 2013 at 15:09 UTC

You are missing a bracket:

```-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W/0.9 ) ,0.045,0

-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W/0.9 ) <---- HERE ,0.045,0

I still dont understand how this works - but it does!

Update: Including testing code that ( I am not sure how ) works!

```
use strict           ;
use warnings         ;

sub EvalPolynominal{
#first parameter gives the polynominal string
#second parameter gives the min-des width
#third parameter gives the short-line length

my \$polynominal=shift;
my \$curWidth=shift;
my \$curLength=shift;

\$polynominal =~ s/W/\$curWidth/g ;#replace W with current value of
++min width
\$polynominal =~ s/L/\$curLength/g ;#replace L with current value of
++ min Length

my \$result ;
eval( \$result = \$polynominal ) ;
# print "\$result\n"; -- PRINTS THE STRING

return eval (\$polynominal); # Returns '0.086765' - WHY?
}

my \$one = EvalPolynominal( '-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (
+W/0.9 ))' ,0.045,0 );

print "\$one\n";

Thanks a lot man, It worked. So stupid of me, couldn't see that thing. I was missing a bracket. Your code worked because of the 2 brackets at the end of the equation and I was missing that one. It happened when I parsed the text into the array and I did something wrong. Thanks again

Re^3: Regarding the polynomial evaluation. Stuck at one part. please helpppp!!
by Anonymous Monk on Jan 22, 2013 at 14:59 UTC

You mean the input for the subroutine ? right ?

Is that what you need help with?

```#!/usr/bin/perl --
use strict; use warnings;
use Carp::Always;
my \$curWidth = my \$curLength = 1;
my \$blah  = q{-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W/0.9 ) ,0.045
+,0 };
print EvalPolynominal( \$blah );

sub EvalPolynominal {
#first parameter gives the polynominal string
#second parameter gives the min-des width
#third parameter gives the short-line length

my \$polynominal = shift;
my \$curWidth    = shift;
my \$curLength   = shift;

\$polynominal =~ s/W/\$curWidth/g ;#replace W with current value of
+min width
\$polynominal =~ s/L/\$curLength/g ;#replace L with current value of
+ min Length
return eval (\$polynominal);
}
__END__
Use of uninitialized value \$curWidth in substitution (s///) at blah li
+ne 18.
main::EvalPolynominal('-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W
+/0.9 ) ,0.045,0 ') called at blah line 6
Having no space between pattern and following word is deprecated at (e
+val 1) line 1.
eval '-0.137 + (0.00479 / (/0.9)) + (2.5593 * (/0.9 ) ,0.045,0
;' called at blah line 20
main::EvalPolynominal('-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W
+/0.9 ) ,0.045,0 ') called at blah line 6

eval is for perl code, not math

build a calculator with marpa dsl or use Math::GSL::Poly but don't use eval

more clearer , still avoid string-eval
```#!/usr/bin/perl --
use strict; use warnings;
my \$blah  = q{-0.137 + (0.00479 / (W/0.9)) + (2.5593 * (W/0.9 ) ,0.045
+,0 };
print EvalPolynominal( \$blah, 1,1 );
sub EvalPolynominal {
#first parameter gives the polynominal string
#second parameter gives the min-des width
#third parameter gives the short-line length

my \$polynominal = shift;
my \$curWidth    = shift;
my \$curLength   = shift;

\$polynominal =~ s/W/\$curWidth/g ;#replace W with current value of
+min width
\$polynominal =~ s/L/\$curLength/g ;#replace L with current value of
+ min Length
local \$@;
my \$ret = eval \$polynominal;
warn "Trying:\n\t\$polynominal\nbut got: \$@\n" if \$@;
return \$ret;
}
__END__

\$ perl -we "  -0.137 + (0.00479 / (1/0.9)) + (2.5593 * (1/0.9 ) ,0.045
+,0 "
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

\$ perl blah
Trying:
-0.137 + (0.00479 / (1/0.9)) + (2.5593 * (1/0.9 ) ,0.045,0
but got: syntax error at (eval 1) line 2, at EOF

Use of uninitialized value in print at blah line 4.

\$ perl -we "  -0.137 + (0.00479 / (1/0.9)) + (2.5593 * (1/0.9 ) ,0.045
+,0 "
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.

Thanks for your reply. Yes, that's exactly what I need to solve. when I am running my script it's giving me these errors

```Use of uninitialized value \$polynominal in substitution (s///) at /hom
+e/vtyagi/perl/EMcheck1/pkg_EMIRcheck/EMIRcheck_tools.pm line 32.
Use of uninitialized value \$polynominal in substitution (s///) at /hom
+e/vtyagi/perl/EMcheck1/pkg_EMIRcheck/EMIRcheck_tools.pm line 33.
Use of uninitialized value \$polynominal in eval "string" at /home/vtya
+gi/perl/EMcheck1/pkg_EMIRcheck/EMIRcheck_tools.pm line 34.

About eval, I have used eval for my last few scripts and it worked very smooth and it's also giving me outputs for the rest of the equations. May be next time i'll keep your advice in my mind and use Math::GSL::Poly. Do you think it could be the problem with format of that particular equation, I mean all the equation have been generated with same format, then what's the problem with this one.

Do you think it could be the problem with format of that particular equation, I mean all the equation have been generated with same format, then what's the problem with this one.

Hmm, perl seems to tell you that its a problem with that (syntax error)

I wouldn't waste my time with string eval

Create A New User
Node Status?
node history
Node Type: note [id://1014681]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2017-11-22 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (327 votes). Check out past polls.

Notices?