Comparing Input to Strings

So I'm writing a relatively simple program that prompts the user for a command, add, subtract, etc, and then prompt for numbers to complete that operation. Everything is written and it compiles fine, but when I enter a command in (add, subtract, etc.) it isn't comparing it properly. Instead of entering the operation branch of the if case, it goes to the invalid command catch I added. Here is part of the code that contains the declaration and the first if statement.

my $command = <STDIN>;
my $counter = 1;
#perform the add operation if the command is add
if (($command eq 'add') || ($command eq 'a')){

    my $numIn = 0;
    my $currentNum = 0;
    #While NONE is not entered, input numbers.
    while ($numIn ne 'NONE'){
        if($counter == 1){
            print "\nEnter the first number: ";
        }else{
            print "\nEnter the next number or NONE to be finished.";
        }
        $numIn = <STDIN>;
        $currentNum = $currentNum + $numIn;

        $counter++;
    }

    print "\nThe answer is: #currentNum \n";

#perform the subtract operation if the command is subtract
}`

Does anyone know why if I enter in add it skips this?

Answers


$command probably still has the new line attached to it, so eq will fail. because "add" != "add\n"

You might consider just checking the first letter of your command, say with a regular expression

$command =~ /^a/i

or use chop on $command to remove the last character.

chop($command)

Need Your Help

Is is possible to override a suggest.js function in wordpress?

php jquery wordpress

Wordpress uses suggest.js to help autocomplete queries, which I am using.

How to use Aether in a org.apache.maven.AbstractMavenLifecycleParticipant

maven maven-3 maven-plugin aether

I followed this example to get the required dependencies injected into my AbtractMavenLifecyleParticipant:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.