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

Please help

by Camerontainium (Initiate)
on Mar 27, 2012 at 16:01 UTC ( #961970=perlquestion: print w/replies, xml ) Need Help??
Camerontainium has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks! I am new to perl and have a question. I am trying to make a palindrome program for class. Our teacher wants us to use chop but I am a little lost. Do I need to join the @userinput array? I tried doing that but everytime I run the program it says palindrome not my else statement. any help is appreciated

my (@userinput, $user, $a, $b, $c, $d, $e, $f, $g, $h, $i); print "\nThis program asks for you to \n enter any nine character sequence and \n then tells you if it is a palidrome!\n\n"; print "Please enter your sequence "; ($user = <>); $a = chop ($user); $b = chop ($user); $c = chop ($user); $d = chop ($user); $e = chop ($user); $f = chop ($user); $g = chop ($user); $h = chop ($user); $i = chop ($user); @userinput = ($i, $h, $g, $f, $e, $d, $c, $b, $a) if ($user = @userinput) { print "PALIDROME!"; } else { print "NOT A PALIDROME!"; }

Replies are listed 'Best First'.
Re: Please help
by roboticus (Chancellor) on Mar 27, 2012 at 16:24 UTC


    I'd first suggest:

    • Read about the chop function so you can see what it does.
    • Read perldoc perlop, paying special attention to the =, == and eq operators.
    • Print the values of your variables, so you can see what the values you're using actually are.

    This should get you part of the way there. Study up, update your code, and post any further questions you have.


    When your only tool is a hammer, all problems look like your thumb.

Re: Please help
by MidLifeXis (Monsignor) on Mar 27, 2012 at 16:23 UTC

    Try adding this to your program after the input statement, and one of the reasons for your results may become clear (make note especially of where the single quotes are located):

    print "'$user'\n";
    You may also want to do a $x = @userinput; print "'$x'\n"; for some more elightenment.


Re: Please help
by jeffa (Bishop) on Mar 27, 2012 at 16:05 UTC

    Why don't you ask your teacher instead? This gives you a great opportunity to show you care about the class and it puts a face on your homework when it comes time to grade it.


    (the triplet paradiddle with high-hat)

      Thank you everyone but I did ask my teacher.

      if ($a == $i && $b == $h && $c == $g && $d == $f) #Final configurat +ion to tell if it is a palindrome { print "PALINDROME!"; } else { print "NOT A PALINDROME!"; }
      That was all i needed to do. I overthought the assignment.
        print "\nThis program asks for you to \n enter any nine character sequence and \n then tells you if it is a palidrome!\n\n";

        Hmm, so the program asks for a character sequence, and you end up using numercal equasions. That will yield a whole lot of palidromes (and warnings if you did use warnings)

        say $a eq $i && $b eq $h && $c eq $g && $d eq $f ? "PALINDROME!" : "No +t a PALINDROME!";

        much simpler:

        $user eq reverse $user and say "You entered a PALINDROME!";

        Enjoy, Have FUN! H.Merijn
        ... hummm...

        I don't see the chop you originally said the teacher wanted you to use.

        And are you sure the teacher didn't say chomp?

Re: Please help
by tobyink (Abbot) on Mar 27, 2012 at 16:58 UTC

    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'
      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'

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

      Cheers, Sören

      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: perlquestion [id://961970]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-03-26 04:10 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (313 votes). Check out past polls.