By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,841 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,841 IT Pros & Developers. It's quick & easy.

Perl variable switching

P: 2
Hello! I won't lie, I'm new to Perl.

I'm trying to switch variables in perl from '1' to '0', and somtimes '2', but it's not working correctly

here's how I think a switch should be preformed:

Expand|Select|Wrap|Line Numbers
  1. if ($input eq "switch them"){
  2. $variable == 1;
  3.  
I think that's wrong, though. I get no error messages, but the output can be drastically different from what I want.

ex:

Expand|Select|Wrap|Line Numbers
  1. if ($input eq "switch them"){
  2. $variable == 1;
  3. print "I have switched the variable\n";
  4.  
At one point, it started printing the number.

assistance would be greatly appreciated
Feb 17 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hello! I won't lie, I'm new to Perl.

I'm trying to switch variables in perl from '1' to '0', and somtimes '2', but it's not working correctly

here's how I think a switch should be preformed:

Expand|Select|Wrap|Line Numbers
  1. if ($input eq "switch them"){
  2. $variable == 1;
  3.  
I think that's wrong, though. I get no error messages, but the output can be drastically different from what I want.

ex:

Expand|Select|Wrap|Line Numbers
  1. if ($input eq "switch them"){
  2. $variable == 1;
  3. print "I have switched the variable\n";
  4.  
At one point, it started printing the number.

assistance would be greatly appreciated
The "$variable == 1" is a comparison saying $variable is equal to 1. If you are trying to set the variable, you only use one equals sign.

Regards,

Jeff
Feb 17 '08 #2

eWish
Expert 100+
P: 971
You are checking to see if $variable is equal to 1, not an assigning it the value of 1.

Expand|Select|Wrap|Line Numbers
  1. if ($input eq "switch them"){
  2. $variable = 1;
  3. print "I have switched the $variable\n";
  4.  
perlop

Edit: Also, you are not printing $variable. You left off the $ sign.

--Kevin
Feb 17 '08 #3

P: 2
thanks! It's helped greatly! Now I can reduce the size of half my programs by 2/3.

thanks again!
Feb 17 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.