[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Please don't Laugh at me...



Don't worry about the laughter; I'm also a relatively new user and I had to
play around with this for a few minutes before I realized what the problems
were.

The first problem is that you can't use an equals sign "=" to compare two
values. You must use "==" to compare numerical values, and "eq" to compare
string values.

The second problem is that $input includes the return character that the
user types to end the input line. You could fix this by saying "if ($input
eq "PAULINE\n")", but a better way would be to  remove this character by
using the chomp() function.

I would also put double-quotes around the two values you want to compare.
I'm not sure it's necessary, but I make a habit of putting quotes around
string variables.

Here's a version that incorporates these three changes:

print ('Please type something: ');
$input = <STDIN>;
chomp ($input);
if ("$input" eq "PAULINE"){
print ("Hola amigo\n")
}else{
print("I don't know you\n")
}

One last source of problems: the user must type in "PAULINE" in upper-case
letters; the code won't match "pauline" or "Pauline". You can get around
this by using a regular expression that looks in $input for the patter
"PAULINE" in any combination of upper- and lower-case letters. This also
allows you to get rid of the chomp, because the string "pauline" can be
found anywhere in the string $input. So the input values "Pauline",
"Pauline\n", "Me llamo Pauline", and even "trampauline" will all work.
Here's a version of the code with a regular expression:

print ('Please type something: ');
$input = <STDIN>;
if ("$input" =~ m/PAULINE/i){
print ("Hola amigo\n")
}else{
print("I don't know you\n")
}

-Tom


At 02:36 PM 8/23/99 -0400, Jorge A. Chica wrote:
>I am relatively new to MacPerl and I would like to know what is wrong
>with my code.
>
>print ('Please type something: ');
>$input = <STDIN>;
>if ($input = PAULINE){
>print ("Hola amigo\n")
>}else{
>print("I don't know you\n")
>}
>
>I want it to print out hola amigo if the STDIN is Pauline.  If not I want
>it to print out I don't know you.  Sorry to bother you guys...
>
>Jorge 
>
>
>===== Want to unsubscribe from this list?
>===== Send mail with body "unsubscribe" to macperl-request@macperl.org
>
>
>

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org