Home: Perl Programming Help: Beginner:
String Operator Problem



iammgg
Novice

May 7, 2017, 10:47 PM


Views: 1894
String Operator Problem

Hi, I know that this is really a basic program and I am still learning to it. I am learning Perl for almost three months now. I have a problem with my program. Can somebody tell me if what's wrong with my code? I tried to run the program but there's something wrong with the output.

It's supposed to be...
Input a string: Hello
Jinx again!

But instead the output would be (the sample that was shown above.) it ended in...
Input a string: Hello
Nice try!

The instruction is: Write a Perl program that has an array with values "Hello", "Hey", "Bye", "Yo" and ask the user for an input. If the input matches one of the elements in the array, print "Jinx again!", else print "Nice try."

Here's my code:

Code
#!/usr/bin/perl 
use strict;
use warnings;

my @words = ("Hello", "Hey", "Bye", "Yo");

my $my_words = @words;

print "Input a string: ";
my $string = <STDIN>;

if ($string eq $my_words) {
print "Jinx again!\n";
}
else {
print "Nice try!\n";
}



(This post was edited by iammgg on May 8, 2017, 3:42 AM)


BillKSmith
Veteran

May 8, 2017, 6:15 AM


Views: 1850
Re: [iammgg] String Operator Problem

A few hints should get you started again.

Your match fails because your input string ends with a newline. Use the function chomp to remove it. With this fix, your code will work for 'Hello', but none of the other choices.

There are several ways you could generalize your code.

Probably the easiest for a beginner is to add a chain of elsif's between your if and your else. Your instructor would probably say that this solution does not 'use the array'.

An advanced solution would use the function 'any' from the module List::Util.

If you are familiar with functions, you can use the built-in function 'grep'.

You could write a 'for' or 'while' loop to test all the words. Be careful. The "Nice try!" case can be a bit tricky.

Feel free to post your new code if you need more help.
Good Luck,
Bill


iammgg
Novice

May 8, 2017, 7:44 PM


Views: 1831
Re: [BillKSmith] String Operator Problem

I tried to do the chomp but it doesn't change the output. I am not that familiar with the 'grep' and 'any' but I will try to look for that functions. Thanks anyway for answering, Mr. Smith.


BillKSmith
Veteran

May 8, 2017, 9:50 PM


Views: 1822
Re: [iammgg] String Operator Problem

Sorry, I overlooked one other mistake.

Code
C:\Users\Bill\forums\guru>type iammgg.pl 
#!/usr/bin/perl
use strict;
use warnings;

my @words = ("Hello", "Hey", "Bye", "Yo");

#my $my_words = @words;
my $my_words = $words[0];

print "Input a string: ";
my $string = <STDIN>;
chomp $string;

if ($string eq $my_words) {
print "Jinx again!\n";
}
else {
print "Nice try!\n";
}

C:\Users\Bill\forums\guru>perl iammgg.pl
Input a string: Hello
Jinx again!

C:\Users\Bill\forums\guru>perl iammgg.pl
Input a string: Bye
Nice try!

Good Luck,
Bill


iammgg
Novice

May 8, 2017, 10:14 PM


Views: 1818
Re: [BillKSmith] String Operator Problem

Thank you so much!