Perl variable switching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suicidal pencil
    New Member
    • Feb 2008
    • 2

    #1

    Perl variable switching

    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:

    Code:
    if ($input eq "switch them"){
    $variable == 1;
    I think that's wrong, though. I get no error messages, but the output can be drastically different from what I want.

    ex:

    Code:
    if ($input eq "switch them"){
    $variable == 1;
    print "I have switched the variable\n";
    At one point, it started printing the number.

    assistance would be greatly appreciated
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by suicidal pencil
    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:

    Code:
    if ($input eq "switch them"){
    $variable == 1;
    I think that's wrong, though. I get no error messages, but the output can be drastically different from what I want.

    ex:

    Code:
    if ($input eq "switch them"){
    $variable == 1;
    print "I have switched the variable\n";
    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

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      You are checking to see if $variable is equal to 1, not an assigning it the value of 1.

      Code:
      if ($input eq "switch them"){
      $variable = 1;
      print "I have switched the $variable\n";
      perlop

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

      --Kevin

      Comment

      • suicidal pencil
        New Member
        • Feb 2008
        • 2

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

        thanks again!

        Comment

        Working...