Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Please help

by tobyink (Abbot)
on Mar 27, 2012 at 16:58 UTC ( #961981=note: print w/ replies, xml ) Need Help??


in reply to Please help

Here's how I'd do it...

use Modern::Perl; say "Enter a word... maybe it's a palindrome...?"; chop(my $input = lc <>); my $reversed = join q(), reverse split q(), $input; say $reversed eq $input ? "yeah" : "nah";

... but don't submit that as-is. Learn from it and write your own.

The chop() function I only use once here, to remove the line-break character from the user input. chop not only tells you what the last character was in the string, it also modifies the string! Thus your comparison at the end will never work, because by the time you've chopped $user nine times, it'll be empty.

(Plus comparing an array and a string using "==" doesn't do what you seem to think it will do.)

Here's a version somewhat closer in spirit to your version:

use Modern::Perl; # Get a line from the user, and remove the # trailing line break say "Enter a word..."; chop(my $user = lc <>); # Keep a copy of the user input because we're # about to totally destroy $user. my $original = $user; # An array that we're going to split the # input into. my @userinput; # Keep going while there's still some input. while (length $user) { # Chop takes a character from the end # of the string. Unshift adds something # to the start of the array. So this # reverses the string as it goes! unshift @userinput, chop $user; } # Now join the array back into a string. my $reversed = join q(), @userinput; if ($original eq $reversed) { say "PALINDROME"; } else { say "NOT A PALINDROME"; }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Please help
Select or Download Code
Re^2: Please help
by Happy-the-monk (Monsignor) on Mar 27, 2012 at 19:11 UTC

    tobyink, reverse works on strings, too, so you can save yourself the trouble and drop both split and join

    Cheers, Sören

Re^2: Please help
by Not_a_Number (Parson) on Mar 27, 2012 at 19:41 UTC

    Your code is broken:

    D:\progs\perl>palindrome.pl Enter a word... noon PALINDROME D:\progs\perl>palindrome.pl Enter a word... ipswich PALINDROME D:\progs\perl>palindrome.pl Enter a word... bolton PALINDROME

    The line

        unshift @userinput, chop $user;

    needs to be changed to

        push @userinput, chop $user;
Re^2: Please help
by CountZero (Bishop) on Mar 27, 2012 at 21:33 UTC
    The chop() function I only use once here, to remove the line-break character from the user input.
    As on many systems the EOL code is two characters long, chop will only remove one and leave you with a "broken" string.

    Use chomp to remove the EOL code instead.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      chop is not great at all, but it was in the project brief. :-)

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961981]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2014-08-27 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (250 votes), past polls