Having trouble getting my interactive perl script to work properly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having trouble getting my interactive perl script to work properly
# 1  
Old 05-05-2017
Having trouble getting my interactive perl script to work properly

So I'm making an interactive perl script, but I can't get it to work properly. I'm trying to make a script that tell the user to input either 'q' or 'Q' to terminate the program, or 'c' to continue it. If they input anything other than those three keys, it should prompt the user again and again until they input an appropriate key. If they input a 'q' or 'Q' the program is terminated. If they input 'c' then it continues to do two functions. The second function I'm saving for later, but the first function involves the program asking the user for two three dimensional coordinates, and then computing and outputting a distance. If any of the users inputs weren't a number it would display an error message. This is the code I have set up for all this, but everytime I input anything in the beginning when it asks for the 'q', 'Q', or 'c' inputs it just repeatedly asks the user to keep inputting something, and never exiting or continuing on to the function, even if I input a 'q', 'Q', or 'c'. Any idea what I need to do to fix this, and if there's anything else wrong with my code?

Code:
use warnings;
use strict;
use Scalar::Util 'looks_like_number';
my $i;
$i=0;
while ($i==0)
{
        print "Welcome to an interactive Perl program. Enter either q or Q to terminate the program or c to continue. Enter your key now: ";
        my $input;
        $input = <STDIN>;
        if (($input ne "q") || ($input ne "Q") || ($input ne "c"))
        {
                print "Welcome to an interactive Perl program. Enter either q or Q to terminate the program or c to continue. Enter your key now: ";
        }
        elsif (($input eq "q") || ($input eq "Q"))
        {
                exit;
        }
        else
        {
                print "Please enter one x coordinate: ";
                my $x1;
                $x1 = <STDIN>;
                print "Please enter one y coordinate: ";
                my $y1;
                $y1 = <STDIN>;
                print "Please enter one z coordinate: ";
                my $z1;
                $z1 = <STDIN>;
                print "Please enter a second x coordinate: ";
                my $x2;
                $x2 = <STDIN>;
                print "Please enter a second y coordinate: ";
                my $y2;
                $y2 = <STDIN>;
                print "Please enter a second z coordinate: ";
                my $z2;
                $z2 = <STDIN>;
                my $answer;
                if (($x1 !~ /^-?0/ && looks_like_number($x1)) && ($y1 !~ /^-?0/ && looks_like_number($y1)) && ($z1 !~ /^-?0/ && looks_like_number($z1)) && ($x2 !~ /^-?0/ && looks_like_number($x2)) && ($y2 !~ /^-?0/ && looks_like_number($y2)) && ($z2 !~ /^-?0/ && looks_like_number($z2)))
                {
                        $answer = sqrt(((($x2)-($x1))**2)+((($y2)-($y1))**2)+((($z2)-($z1))**2));
                        print "The distance between the two points is $answer";
                }
                else
                {
                        print "Error, you must only input numbers.";
                }
        }
}


Last edited by RudiC; 05-06-2017 at 02:55 AM..
# 2  
Old 05-06-2017
My perl is non-existent, but I see
- the $i variable not being modified anywhere in the - thus infinite - loop
- the $input test always being true due to the || operator
# 3  
Old 05-06-2017
To clarify RudiC's 2nd remark:
Code:
if (($input ne "q") || ($input ne "Q") || ($input ne "c"))

Any variable is always not equal to "q" or not equal to "Q" etc..
The && operator should be used here..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-06-2017
I changed the || to && but I'm still getting the same thing where it just continually prompts the user to enter a key, even if they enter in 'q', 'Q', or 'c'
# 5  
Old 05-06-2017
Quote:
Originally Posted by Eric1
I changed the || to && but I'm still getting the same thing where it just continually prompts the user to enter a key, even if they enter in 'q', 'Q', or 'c'
Remove the ENTER or RETURN key out of the captured input.

Code:
my $input = <STDIN>;
chomp $input;
if (($input ne "q") && ($input ne "Q") && ($input ne "c")) {
   ...
}

or, ignore anything else after the first character.
Code:
my $input = <STDIN>;
if($input !~ /^[Qqc]/) {
   ...
}

# 6  
Old 05-06-2017
Thanks Aia! It's working now! I'll either update this thread or start a new one when I'm nearing the end of the full program, thanks again for the help!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can't get mv to work properly

Trying to use mv in a shell script but for some reason this does not work: for f in *.wav;do mv $f $f.bwf;done I get this: usage: mv source target mv source ... directory So it's like I'm using 'mv' wrong but I can't see how. This works so the contens of the folder is read... (4 Replies)
Discussion started by: Oortone
4 Replies

2. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

3. Red Hat

TAB button doesn't work properly

Hi, I have list of files under a directory as shown below. /home/root -rw-r--r-- 1 user 0 Aug 27 06:08 rough.txt -rw-r--r-- 1 user 0 Aug 27 06:08 test.sh Now when i use to read the rough.txt, i did cat r and then pressed TAB button. Below is the resulting string after pressing TAB... (2 Replies)
Discussion started by: vel4ever
2 Replies

4. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

5. UNIX and Linux Applications

Gvim doesn't work properly

Hello All, I am using gvim ( redhat linux machine). backspace doesnot work properly. can some boby suggest a solution ? i have checked with older version. backspace works in it. Thanks Shiv (1 Reply)
Discussion started by: shiv.emf
1 Replies

6. Shell Programming and Scripting

A script doesnt work properly when crontab

Hi, I have a script which does a tar and sends it to another server as backup. Script is as below # Locations to be backed up. Seperate by space BACKUP_LOCATIONS=/repos/subversion BACKUP_BASE_FOLDER=/bakpool BACKUP_FILE_NAME_ROOT=svn-backup START_TIME_DISP=`date` START_TIME=`date... (11 Replies)
Discussion started by: digitalrg
11 Replies

7. UNIX for Dummies Questions & Answers

Big Trouble At Work!

People are misbehaving and doing things on the network they shouldn't be while at work, and it's killing the server. So the boss wants a plan on how to deal with these issues ASAP. What i need to know is how to use iptables to filter traffic coming to a specific ip... to say "this is allowed to... (1 Reply)
Discussion started by: ShawnaB
1 Replies

8. Solaris

Solaris Network doesn't work properly

Hi to all! I want to learn step by step easily how to configure my Solaris for network. I know alot about Solaris Network configuration. But I have some problems. When I install Solaris, and I plug-in my network cable to Solaris. Then I run: ifconfig -a plumb then I do ifconfig bge0 dhcp... (7 Replies)
Discussion started by: SecureXCode
7 Replies

9. HP-UX

pstat_getdisk() call doesn’t work properly in HPUX 11.31 (11i V3)

As per the man page, pstat_getdisk() call returns the number of instances, which could be 0 upon successful completion, otherwise a value of -1 is returned. Please have a look at this sample program -> #include <stdio.h> #include <sys/pstat.h> int main() { int j = 0, ret; struct... (2 Replies)
Discussion started by: sandiworld
2 Replies
Login or Register to Ask a Question