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:
Two user inputs (String and Integer)

 



iammgg
Novice

May 29, 2017, 8:12 PM

Post #1 of 3 (1241 views)
Two user inputs (String and Integer) Can't Post

At first, I tried to do a for loop but it kept repeating the output (Input number is too big) same as when I used foreach loop.

I think there's something wrong with my condition statement, isn't it?


Code
#!/usr/bin/perl 
use strict;
use warnings;

=pod
Write a perl program that will ask for 2 inputs.
The first input is a string and second input
is an integer.
Based from the second input, print out
the letter of that position.

Example:
Input a string: Hello
Input a number: 2
The letter is 'e'

Input a string: Hello
Input a number: 6
Input number is too big
=cut

print "Input a string: ";
my $string = <STDIN>;
chomp($string);

print "Input a number: ";
my $num = <STDIN>;
chomp($num);

my $letter = $num;

if($string eq $num) {
print "The letter is '$letter'\n";
}
else {
print "Input number is too big\n";
}


Thanks in advance.


BillKSmith
Veteran

May 30, 2017, 4:02 AM

Post #2 of 3 (1213 views)
Re: [iammgg] Two user inputs (String and Integer) [In reply to] Can't Post

This appears to be a homework assignment so I will not give you the answer, but I will try to help.

This assignment cannot be done without using one or more of Perl's string manipulation functions (index, length,split, substr).

Hint: A loop will not help you. The only test required is the one for "too big". If it is not too big, the number is the index of the letter.

If you are studying arrays, you probably want to split the string into an array of characters. If you are studying strings, use the functions with no array at all.

UPDATE: Added link for length.
Good Luck,
Bill

(This post was edited by BillKSmith on May 30, 2017, 8:29 AM)


Laurent_R
Veteran / Moderator

May 30, 2017, 7:38 AM

Post #3 of 3 (1208 views)
Re: [iammgg] Two user inputs (String and Integer) [In reply to] Can't Post

Yes, I agree with Bill, this is obviously homework assignment, so I'll give you just some clues.


Code
my $letter = $num;

does not make sense. $num is a number, say 2 in the first example provided in the pod section. It is not "e", which is what you want. So you need some function to extract the second character of the string. Look at the documentation on the substr function.


Code
if($string eq $num) {

does not make more sense.

Still taking the first example supplied in the pod section, $string would contain "Hello" and $num is 2. They are not equal, and therefore your code branches to the else block. You want to compare $num with the length of the string. Look at the documentation for the length function.

In addition, you don't want to compare for equality, you want to know whether the number supplied is smaller than or equal to the number, or if it is larger.

And the $letter assignment should be done only if comparing the length of the string with the number provided the expected result.

I hope this will get you going without spoiling too much the purpose of this homework.

 
 


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

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