CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
String Operator Problem

 



iammgg
Novice

May 7, 2017, 10:47 PM

Post #1 of 5 (842 views)
String Operator Problem Can't Post

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

Post #2 of 5 (798 views)
Re: [iammgg] String Operator Problem [In reply to] Can't Post

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

Post #3 of 5 (779 views)
Re: [BillKSmith] String Operator Problem [In reply to] Can't Post

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

Post #4 of 5 (770 views)
Re: [iammgg] String Operator Problem [In reply to] Can't Post

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

Post #5 of 5 (766 views)
Re: [BillKSmith] String Operator Problem [In reply to] Can't Post

Thank you so much!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives