Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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'

Replies are listed 'Best First'.
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.


    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'
Re^2: Please help
by Happy-the-monk (Canon) 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 (Prior) on Mar 27, 2012 at 19:41 UTC

    Your code is broken:

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

    The line

        unshift @userinput, chop $user;

    needs to be changed to

        push @userinput, chop $user;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961981]
[choroba]: A colleague showed me Lightning Maps today
[marto]: very nice

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2018-05-23 21:37 GMT
Find Nodes?
    Voting Booth?