in reply to elsif failing

Using the proper comparison operator makes a great difference, you're comparing strings hence your operator to use is "eq", if you were comparing numbers however, you would have used "==" instead, this said, you know by now that "=" is the assignment operator.

using:

use strict; use warnings;
at the top of each program saves you a lot of trouble, notice, when the strictures are on you would have to declare your variables using "my" the first time the variable is brought to existence, this has got to do with proper scoping and serves also to save you headaches from typing errors in variable names

Now here is how your program would look like after corrections. Notice I did not use escaping by \n to put an apostrophe in you're because it is (i.e ' ) not one of Perl's metacharacters.

#!/usr/local/bin/perl use strict; use warnings; print "What is your name?\n"; chomp(my $name = <STDIN>); if ($name eq 'larry') { print "You're Larry!\n" } elsif ($name eq 'moe') { print "You're Moe!\n" } elsif ($name eq 'curly') { print "You're Curly!\n" }
Just like GrandFather said, using a data structure like hashes is the answer to avoid repetitive code-writing. Another thing, Perl boasts of a capability called TIM TOWDY which means "There is more than one way to do it", and here is another way to do your program in addition, using another type of data structures called arrays to hold the names for you and the rest of the code just checks the condition when the name exists and prints it:
#!/usr/local/bin/perl use strict; use warnings; my @names =qw(larry moe curly); print "What is your name\n"; chomp(my $name = <STDIN>); if(grep {/$name/} @names){ print "you're @{[grep {/$name/} @names]}!\n"; }else{ print "I don't know this name\n"; }
Update: Hashes are more flexible to work with as GrandFather duly explained. I wish you a happy Perl programming, it is just real fun to learn Perl.

Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: elsif failing
by GrandFather (Saint) on Sep 22, 2009 at 07:11 UTC

    A more interesting 'nother way to do it' is by dispatching methods. It's still a hash lookup under the hood, but is a fine technique to be aware of for some situations (adding verbs to a parser for example). Consider:

    use warnings; use strict; print "What is your name?\n"; chomp (my $name = ucfirst <STDIN>); my $obj = bless {}; my $call = $obj->can ("name$name"); if ($call) { $call->(); } else { print "I can't handle anyone by the name $name.\n"; } sub nameLarry { print "Hi Larry. Welcome\n"; } sub nameMoe { print "Hi Moe. Late again I see!\n"; } sub nameTim { print "Hi TIMTOWTDI. Still pursuing the alternative life style?\n" +; }

    The OP should note that while using an array and grep is an alternative technique, it's not a very good alternative because it doesn't scale well. That is, if there are a lot of names and the lookup needs to be done many times the 'linear search' that grep uses will slow processing down substantially. A small improvement can be made by using a for loop and using last to exit the loop as soon as a match is found, but that really doesn't redeem the array lookup where a hash or method dispatch could have been used.


    True laziness is hard work