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:
Why doesn't this work?

 



sttring
Deleted

Jun 2, 2001, 6:52 PM

Post #1 of 10 (3965 views)
Why doesn't this work? Can't Post


Code
print "Choose a word: "; 
$word = <STDIN>;
$revword = reverse($word);
if ($word eq $revword) {
print "This is a palindrome"
} else {
print "This is not a palindrome"
}

This code should work though it doesn't for some reason. It always prints "This is not a palindrome", no matter what the word is. What's the problem?

Thanks,
sttring



Pro_4
User

Jun 2, 2001, 7:11 PM

Post #2 of 10 (3961 views)
Re: Why doesn't this work? [In reply to] Can't Post

Hey,

I tested your script and this is what i did to make it work:

Code
#!/USR/BIN/PERL -w 
print "Choose a word: ";
$word = <STDIN>;
$revword = reverse($word);
if ($word = $revword) {
print "This is a palindrome"
} else {
print "This is not a palindrome"}
[\code]

the only thing that had to be changed was = instead of eq. Eq compares numbers not words so that is what was wrong.

:)


Yes i am a palm tree. What are you plantist or something!?


freddo
User

Jun 2, 2001, 7:16 PM

Post #3 of 10 (3961 views)
Re: Why doesn't this work? [In reply to] Can't Post

Hi Pro,

here's my try at it (in fact you forgot to remove the new line):

Code
#!/usr/bin/perl 
print "Choose a word: ";
chomp ($word = <STDIN>);
$revword = reverse($word);
print "This is ", ($word eq $revword?"":"not "), "a palindrome."



freddo

;---
ygjohn: sometimes. you talk too much.. hahaha..


Pro_4
User

Jun 2, 2001, 7:26 PM

Post #4 of 10 (3960 views)
Re: Why doesn't this work? [In reply to] Can't Post

Ah bah humbug,

Stop trying to out do me Freddo. Either way it works so haha!

(this is what happens after programming for about 12 hours straight)

Yes i am a palm tree. What are you plantist or something!?


freddo
User

Jun 2, 2001, 7:41 PM

Post #5 of 10 (3959 views)
Re: Why doesn't this work? [In reply to] Can't Post

Re Pro,

it's strange, because if i dont chomp i doesnt work? here's the code i use without chomp:

Code
print "Choose a word: "; 
$word = <STDIN>;
$revword = reverse($word);

print "This is ", ($word eq $revword?"":"not "), "a palindrome."

and here is what i have:

Code
freddo@FANTA /cygdrive/h/scripts.pl/perlguru/pro4 
$ perl palin2.pl
Choose a word: hello
This is not a palindrome.
freddo@FANTA /cygdrive/h/scripts.pl/perlguru/pro4
$ perl palin2.pl
Choose a word: abba
This is not a palindrome.

??? (tried with cygnus'cygwin)

freddo


;---
ygjohn: sometimes. you talk too much.. hahaha..


sttring
Deleted

Jun 2, 2001, 8:15 PM

Post #6 of 10 (3956 views)
Re: Why doesn't this work? [In reply to] Can't Post

Pro, I tested your code and it does not work. Instead of always printing "This is not a palindrome" it now always prints "This is a palindrome".

Also what do you mean by eq compares numbers? Eq compares strings. == compares numbers. = isn't used to compare anything.

Regards,
sttring



Pro_4
User

Jun 2, 2001, 8:45 PM

Post #7 of 10 (3951 views)
Re: Why doesn't this work? [In reply to] Can't Post

hmm, i see arg, well my vision is getting blurry and my mind is a little twisted about so plz excuse me if i do things wrong. I am being sapped off a my life forces from this massive project i am working on and it is starting to hurt my head. Yes your right sttring. I was only testing like madam because before it would only return this is not a palindrome. Anyways freddo answered the correct way so i dont feel to bad.

:)

Yes i am a palm tree. What are you plantist or something!?


Mortimer
journeyman

Jun 2, 2001, 9:08 PM

Post #8 of 10 (3948 views)
Re: Why doesn't this work? [In reply to] Can't Post

That's because when you press enter to feed the program with your input, you tack a \n on to the end of your string.

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com




freddo
User

Jun 2, 2001, 9:19 PM

Post #9 of 10 (3944 views)
Re: Why doesn't this work? [In reply to] Can't Post

Hey Mortimer,

Well i know about that \n on my input (see my previous post), in fact, i was wondering how Pro4 could make it work without cho(m)p? (and we got the answer, he's kinda tired)....

btw, you made a quite elegant solution for the even($num) sub, i wasnt working on floats just because i wanted to show the point about binary operations (in asm a shift is quite faster than a multiply or a divide).

freddo

;---
ygjohn: sometimes. you talk too much.. hahaha..


Jasmine
Administrator / Moderator

Jun 3, 2001, 7:05 AM

Post #10 of 10 (3932 views)
Re: Why doesn't this work? [In reply to] Can't Post


In Reply To
the only thing that had to be changed was = instead of eq. Eq compares numbers not words so that is what was wrong.

Nope.

eq tests for word equality and == (two equal signs) tests for numeric equality.


 
 


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

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