Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Conditionally Changing a Variable

by Anonymous Monk
on Dec 17, 2001 at 22:53 UTC ( #132610=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Although it should work from a logic standpoint, I don't seem to be able to use the following. Why? c$ = 0 if (c$ < 0); c$ will remain the same as it originally was.

Replies are listed 'Best First'.
Re: Conditionally Changing a Variable
by Rhose (Priest) on Dec 17, 2001 at 23:01 UTC
    Might want to move the $
    $c=0 if ($c < 0);
      Typo. That's what I get for trying to do something quickly before lunch. Shades of old Basic programming shinning through. However, I could not get this to work last night....now it does....at work. Maybe my Perl interpreter on the home machine is broken. I had a similar problem in the past with variables such as: $n = $filenameB if ($n = $filenameA);
        I had a similar problem in the past with variables such as: $n = $filenameB if ($n = $filenameA);

        No wonder you are having problems with this, two errors in the same line :). First of all you are doing an assignment in the if-condition so this should be '==' instead of '='. And second '==' only works for numbers which is probably not what you want ($filename !!). You should use 'eq' instead. $n = $filenameB if ($n eq $filenameA);

        -- Hofmator

Re: Conditionally Changing a Variable
by joealba (Hermit) on Dec 17, 2001 at 23:05 UTC
    Works fine for me, without the typo. Was that a cut-n-paste error, or is that your actual code (with the c$ instead of $c in your conditional)?
    my $c = -5; print "C is now $c\n"; $c = 0 if ($c < 0); print "C is now $c\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://132610]
Approved by root
help
Chatterbox?
[choroba]: Corion Sounds reasonable
[Corion]: choroba: Yeah - I basically have the same for regenerating README and README.mkdown already, except that I do that in Makefile.PL, but I guess one or the other thing should somehow work ;)
[ambrus]: was this bug: https://rt.cpan. org/Public/Bug/ Display.html?id= 59814
[Corion]: ambrus: Oh - that one would be much harder to automate... The SYNOPSIS section should mostly be a runnable program IMO, but I write only small snippets in my documentation for single functions/methods, and creating the appropriate environment for ...
[Corion]: ... those in an automated fashion seems somewhat hard to me. Although it should do wonders for the test coverage ;)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-02-27 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (384 votes). Check out past polls.