Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Perl Palindrome

by hushbaby (Initiate)
on Apr 15, 2014 at 04:00 UTC ( [id://1082295]=perlquestion: print w/replies, xml ) Need Help??

hushbaby has asked for the wisdom of the Perl Monks concerning the following question:

This is my code:
print "\nThis program accepts a 9 character string from you the user a +nd determines whether or not It is a palindrome, printing the result to the screen. \n"; print "\nPlease Enter in a 9 character item: "; ; my ($a, $b, $c, $d, $e, $f, $g, $h, $i); chomp ($a=<>); chomp ($b=<>); chomp ($c=<>); chomp ($d=<>); chomp ($e=<>); chomp ($f=<>); chomp ($g=<>); chomp ($h=<>); chomp ($i= +<>); if ($a eq $i && $b eq $h && $c eq $g && $d eq $f && $e eq $e ){ print "PALINDROME!\n"; } else{ print "NOT A PALINDROME! \n"; }

I have to enter the number individually, I am trying to find a way to enter the 9 numbers all at once.

But in this case I have to enter one number and press enter on and on till I reach the 9th number.

Please can anyone help me to figure out a way to input the number in this format.

Print" please enter 9 number: \n"

123456789

Not Palindrome.

or Print" please enter 9 number: \n"

123454321

Palindrome

In my case I have to enter:

1

2

3

4

5

6

7

8

9

Not Palindrome.

I dont want it to be this way. Please I will apreciate any help.

Replies are listed 'Best First'.
Re: Perl Palindrome
by kcott (Archbishop) on Apr 15, 2014 at 04:18 UTC

    G'day hushbaby,

    Welcome to the monastery.

    You just need a single variable to capture user input.

    Use length if you need to check the number of characters entered.

    Use reverse to check for a palindrome.

    Here's the guts of what you want:

    #!/usr/bin/env perl use strict; use warnings; print 'Enter string: '; chomp(my $string = <>); print $string eq reverse($string) ? 'Yes' : 'No', "\n";

    Sample runs:

    Enter string: 123 No
    Enter string: 121 Yes

    I'll leave you to flesh that out for your homework(?).

    -- Ken

Re: Perl Palindrome
by AnomalousMonk (Archbishop) on Apr 15, 2014 at 04:18 UTC
Re: Perl Palindrome
by atcroft (Abbot) on Apr 15, 2014 at 04:22 UTC
      Is there a way of doing the required action without the usage of reverse or split however...

        Is there a way of doing the required action without the usage of reverse or split however...

        I'll tell you when your test/interview/homework deadline has passed

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1082295]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found