Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Weird Perl Rule I'm Probably Not Following

by moltar512 (Sexton)
on Oct 14, 2005 at 16:41 UTC ( [id://500296]=perlquestion: print w/replies, xml ) Need Help??

moltar512 has asked for the wisdom of the Perl Monks concerning the following question:

Hi this is my first post here please don't shoot if i do something wrong ;) This is what i'm doing: I'm trying to write a method that solves for the powers of complex numbers. I know that there are modules available for this, but for my current situation, using those modules aren't practical, so I am forced to write my own. this is my code:
#FINDS THE COMPLEX ARGUMENT #PRETTY SURE THIS WORKS TOO sub argComplex { my $value1, $real1, $complex1; $value1 = @_[0]; @construct = &constructComplex($value1); $real1= @construct[0]; $complex1 = @construct[2]; return atan2($complex1, $real1); } $real1 = 1; $z = 1 + i; #LOOK HERE #RETURNS ZERO #I DON'T KNOW WHY #BY ITSELF, argComplex($z) RETURNS 0.7854, WHICH IS RIGHT ACCORDING TO + MATLAB print $real1 * argComplex($z);
when the print function goes through, it prints 0 instead of the right answer. I'm convinced there is some weird rule in Perl that I'm not following.
If you get it to print $real1, it prints 1
If you get it to print argComplex($z), it prints .7854
but if you get it to print $real1 * argComplex($z), it prints Zero
Please Help
Thanks,
Donnell

Replies are listed 'Best First'.
Re: Weird Perl Rule I'm Probably Not Following
by Roy Johnson (Monsignor) on Oct 14, 2005 at 17:01 UTC
    The argComplex sub changes the values of $real1, because your my is not parenthesized. You need
    my ($value1, $real1, $complex1);
    to declare the three variables.

    Caution: Contents may have been coded under pressure.
      hmm.. okay what was happening before? which of the variables was the my affecting? just the first in the string of variables??
        Yes. my takes a variable or a parenthesized list of variables. You would eventually have located that bug if you had at the top of your program
        use strict; use warnings;
        strict would have complained about variables not being declared, and warnings would have warned you about using things like $s = @var[1].

        Caution: Contents may have been coded under pressure.
      btw.. you just fixed my problem!! THANKS!!
Re: Weird Perl Rule I'm Probably Not Following
by kutsu (Priest) on Oct 14, 2005 at 17:01 UTC

    Your code contains several errors, which is to be expected of someone new to perl, it's $array[fieldnum] not @array[fieldnum] - add "use warnings;" and "use strict;" and you'll find several more (see Use strict warnings and diagnostics or die for more). A bigger question in my mind is why can't you use modules?

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      i'm trying to add complex number support for a open source program called Lon-Capa made by Michigan State University. Lon-Capa is an online homework submission system. they used perl to make it, and in the way that you define your problems. They use perl script to define how problems are randomly generated. I can't just use a module to make lon-capa understand complex numbers because if i did my problems wouldn't be compadible with lon-capa anymore. What i can do, however, is write a perl library file to do any math that capa doesn't already do. unless you guys know a better way.. hahahahhaa
        What i can do, however, is write a perl library file to do any math that capa doesn't already do. unless you guys know a better way.. hahahahhaa
        Um, use a *prewritten* "perl library file" to do the math that "capa" doesn't already do?
Re: Weird Perl Rule I'm Probably Not Following
by fishbot_v2 (Chaplain) on Oct 14, 2005 at 17:15 UTC
    I'm convinced there is some weird rule in Perl that I'm not following.

    Yep. That rule is use warnings;

    use warnings; my $value1, $real1, $complex1; __END__ Parentheses missing around "my" list at - line 3. Useless use of a variable in void context at - line 3. Useless use of a variable in void context at - line 3.
Re: Weird Perl Rule I'm Probably Not Following
by tphyahoo (Vicar) on Oct 14, 2005 at 16:50 UTC
    That's a lot of code and hard to understand. Could you somehow narrow your question down so it only has the code that your program is stumbling over? You'll probably get better answers then. Good luck, and welcome to perlmonks!

    Oh, and to make it easier to read, you might want to run your perl program through perltidy first to make it easier to read. But that's sort of of side-importance; the most important thing is to reduce the amount of code you posted.

    2005-10-14 jdporter restored title b0rkage that left only 'Re:'

Re: Weird Perl Rule I'm Probably Not Following
by eric256 (Parson) on Oct 14, 2005 at 20:50 UTC

    Cleaned up the code some, I know many people learn by example so here is how you might improve the code. Declare all variables you want to use with "my", especialy inside of the sub. Grab arguments to the sub with shift (this is iffy and probably good arguments both ways. The important thing it to be consistent.) Indent code inside a sub so that you can tell the scope of that sub (where variables declared inside it with my will retain there uniqness for lack of a better workd.)

    sub argComplex { my $value1 = shift; # shift along acts on @_ so its the same as @ +_[0]; my @construct = &constructComplex($value1); my $real1 = @construct[0]; my $complex1 = @construct[2]; return atan2($complex1, $real1); } my $real1; my $z; $real1 = 1; $z = 1 + i; print $real1 * argComplex($z);

    P.S. It is easier to help when you provide a working example. In this case people spoted the error but if you break the problem down into a working example to show peopl, 90% of the time you will find your error before posting! ;)


    ___________
    Eric Hodges
      my $value1 = shift; # shift along acts on @_ so its the same as @_[0];

      Not really.
      shift will remove the first element from @_
      @_[0] leaves it in place

        Sorry you took that a little literaly. I meant that it filled the same position as @_[0] did in his code. Which IS true. I probably should have made that clearer.


        ___________
        Eric Hodges

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-19 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found