Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
The stupid question is the question not asked
 
PerlMonks  

Calculating polynomials and pushing them into an array. Stuck at one part. please helpppp!!

by freekngeek (Acolyte)
on Jan 22, 2013 at 14:28 UTC ( #1014677=perlquestion: print w/ replies, xml ) Need Help??
freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am calculating polynomial values and then pushing those values into an array. For execution I am using this subroutine :

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); }

And this is I am pushing values to the array.

push @CompareValues,&EvalPolynominal($$lvlhash{$level}{'POLY_BASED_EM_ +DC'}{'EM_POLY'}{11},$$refhash{$level}{'MINDESIGNWIDTH'},0); print ("size ".@CompareValues. "\n");

I am reading all the polynomial equations from the text file and all of them are they stored in hashes. So that means 11 is one equation, 21 is another and so on. I am using the above script for 3 different equations, I am getting output from the last two and undef from the first one which is 11. Here's the output when I do print dumper (\@CompareValues);

$VAR1 = [ undef, '0.048648', '0.048648' ];

I guess it's easy to do but I have no idea what am I doing wrong here and what's wrong because I am getting values for other equation. I would like to know what's wrong with this code, I'll be glad if some of few could help me. Thanks.

Comment on Calculating polynomials and pushing them into an array. Stuck at one part. please helpppp!!
Select or Download Code
Re: Regarding the polynomial evaluation. Stuck at one part. please helpppp!!
by Anonymous Monk on Jan 22, 2013 at 14:30 UTC

      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

        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

        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";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1014677]
Approved by muba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-04-20 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls