Slight error with my perl script that I could use some help on


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Slight error with my perl script that I could use some help on
# 1  
Old 05-09-2017
Slight error with my perl script that I could use some help on

So I have a perl script that prompts the user to enter either q or Q to exit the program or c to continue said program. If the user inputs anything other than those three keys they will be prompted again and again for an appropriate input. My script works for the most part except for one small oddity. Here's an example- if the user inputs a, they will be prompted again. They input a again, they are prompted once more. They input q and the program exits. Works fine right? I run my script again and I change my inputs a little. This time I input a first, the user is prompted again. Now I input q, but the program doesn't exit, it prompts me yet again. If I input q now, it will exit. This oddity also occurs if it's Q or c as well instead of q. This is the oddity I want to fix, can anyone help? Here is the bulk of my code that deals with this small problem-

Code:
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>;
chomp $input;
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: ";
                $input = <STDIN>;
                chomp $input;
        }
elsif (($input eq "q") || ($input eq "Q"))
        {
                exit;
        }
else
        {

the parts after else aren't important, as I've tested them and they work fine. It's only the small oddity that I described above that I need assistance with

Last edited by jim mcnamara; 05-09-2017 at 09:03 PM.. Reason: code tag correction
# 2  
Old 05-09-2017
Well something has to loop back to the prompt - you did not show that part. I can't offer help for something I cannot see.

Please post the whole script.

Thanks
# 3  
Old 05-09-2017
Code:
#!/usr/bin/perl

use warnings;
use strict;
use Scalar::Util 'looks_like_number';
no strict "subs";
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>;
        chomp $input;
        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: ";
                $input = <STDIN>;
                chomp $input;
        }
        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 \n";
                }
                else
                {
                        print "Error, you must only input numbers. \n";
                }
                my %aminoacid;
                @aminoacid{qw(uuu uuc)} = (Phe) x 2 ;
                @aminoacid{qw(uua uug cuu cuc cua cug)} = (Leu) x 6 ;
                @aminoacid{qw(auu auc aua)} = (Ile) x 3 ;
                @aminoacid{qw(aug)} = (Met) x 1 ;
                @aminoacid{qw(guu guc gua gug)} = (Val) x 4 ;
                @aminoacid{qw(ucu ucc uca ucg agu agc)} = (Ser) x 6 ;
                @aminoacid{qw(ccu ccc cca ccg)} = (Pro) x 4 ;
                @aminoacid{qw(acu acc aca acg)} = (Thr) x 4 ;
                @aminoacid{qw(gcu gcc gca gcg)} = (Ala) x 4 ;
                @aminoacid{qw(uau uac)} = (Tyr) x 2 ;
                @aminoacid{qw(uaa uag uga)} = (TER) x 3 ;
                @aminoacid{qw(cau cac)} = (His) x 2 ;
                @aminoacid{qw(caa cag)} = (Gln) x 2 ;
                @aminoacid{qw(aau aac)} = (Asn) x 2 ;
                @aminoacid{qw(aaa aag)} = (Lys) x 2 ;
                @aminoacid{qw(gau gac)} = (Asp) x 2 ;
                @aminoacid{qw(gaa gag)} = (Glu) x 2 ;
                @aminoacid{qw(ugu ugc)} = (Cys) x 2 ;
                @aminoacid{qw(ugg)} = (Trp) x 1 ;
                @aminoacid{qw(cgu cgc cga cgg aga agg)} = (Arg) x 6 ;
                @aminoacid{qw(ggu ggc gga ggg)} = (Gly) x 4 ;
                print "Plese enter a 3 character long codon using only the characters u a c and g to find the associating amino acid: ";
                my $codon;
                $codon = <STDIN>;
                chomp $codon;
                if ($codon =~ /^[uagc]{3}$/)
                {
                        print "$aminoacid{$codon} \n";
                }
                else
                {
                        print "Error, you must only input a three character long codon using only the characters u a c and g. \n";
                }
                $i++;
        }
}


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 05-10-2017 at 03:12 AM.. Reason: Changed ICODE to CODE tags.
# 4  
Old 05-10-2017
I would suggest you take a look at your logic, verifying where in the code you think you are after each input. You'll might be surprise to find yourself in a different place.

Can you articulate the purpose of the following portion of your code?

Code:
my $i;
$i=0;
while ($i==0) {
    ...
    $i++;
}

# 5  
Old 05-10-2017
This script is the same as the interactive perl script that I've made a few threads about before. That whole portion is the purpose of my script.

That said, I went in and mucked about some more and fixed the issue. Thanks for the help either way everyone
# 6  
Old 05-10-2017
Quote:
Originally Posted by Aia
I would suggest you take a look at your logic, verifying where in the code you think you are after each input. You'll might be surprise to find yourself in a different place.

Can you articulate the purpose of the following portion of your code?

Code:
my $i;
$i=0;
while ($i==0) {
    ...
    $i++;
}

Quote:
Originally Posted by Eric1
This script is the same as the interactive perl script that I've made a few threads about before. That whole portion is the purpose of my script.

That said, I went in and mucked about some more and fixed the issue. Thanks for the help either way everyone
Here's the bottom line of that construction.
It will iterate only once, therefore is not necessary as a loop.

Also, if you would accept another suggestion.
Instead of
Code:
@aminoacid{qw(ugg)} = (Trp) x 1 ;

this
Code:
$aminoacid{'ugg'} = 'Trp';

# 7  
Old 05-10-2017
Ahh okay, thank you for the tips and help Aia
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Slight variation from the desired results

Hello, I am writing a small one liner script to display the tables in my database. I am working with Centos 5.5 and postgresql the command is "psql -c "\dt" | awk '{print$3}'" I just want the 3rd column from the result set, but now the problem is I am getting the third column but with... (3 Replies)
Discussion started by: nnani
3 Replies

2. Programming

getting error in my perl script

hi Here is my code written to identify the particular position which is after a string (chr*). my input file looks some thing like this aaanbb:anhn:iuopl:12345 chr1 12345 asnmkol * # kjiiii.....anmkij:lpolk:lopll:abnnj chr5 123222 polko * dddfgg .... aaanbb:anhn:iuopl:aanjuj chr2 44345 asnmkol... (1 Reply)
Discussion started by: anurupa777
1 Replies

3. Linux

Slight Linux Upgrade

Hello Ya'all: I hope Zaxxon is still around. I read a posting about compiling/updating the kernel from source. I'm doing a very specific upgrade, and am wondering if there is anything different or if there's an easy way to do this: I am using kernel version 2.6.18-92, and have done some... (1 Reply)
Discussion started by: Statue
1 Replies

4. Shell Programming and Scripting

How to merge two files with a slight twist

Hi, a brief introduction on the soundex python module(english sound comparison): import soundex.py a = "neu yorkk" b = "new york city" print soundex.sound_similar(a, b) output: 1 Suppose I want to merge two files, called mergeleft.csv and mergeright.csv Mergeleft.csv: ... (0 Replies)
Discussion started by: grossgermany
0 Replies

5. Shell Programming and Scripting

Need help with a slight modification to my PERL script

Hi all, So I have a script that reads a file called FILEA.txt and in that file there are several columns. The ones that are most important are the $name $start and $stop. So currently the script takes values between the start and stop (inside) by using a program called fastamd. But what I... (4 Replies)
Discussion started by: phil_heath
4 Replies

6. Shell Programming and Scripting

Perl script error

Hi all I keep getting a segmentation fault error while running the script below. #!/usr/bin/perl -w use CGI ':standard'; use GD::Graph::pie; use strict; use warnings; sub trim($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; ... (4 Replies)
Discussion started by: pietie
4 Replies

7. Shell Programming and Scripting

CMP two files with slight difference and return code

I am comparing two files which are identical except for the timestamp which is incorporated within the otherwise same 372 bytes. I am using the command: cmp -s $Todays_file $Yesterdays_file -i 372 When I run the command without the -i 372 it shows the difference i.e. the timestamp.... (5 Replies)
Discussion started by: gugs
5 Replies

8. UNIX for Dummies Questions & Answers

SCCS - A slight scripting issue

I used %H%M for hours and minutes within a date variable, to latch the date/time onto the end of a file, the script it was in is now under SCCS control and the %H% is a predefined parameter for SCCS, so it tags a date with a "/" character in it. Is there a way to tell SCCS to ignore anything... (0 Replies)
Discussion started by: tangent
0 Replies

9. UNIX for Dummies Questions & Answers

Having a slight problem!?

having a slight problem. any clues would help. Can't seem to get any output when I run a simple echo script. grex.cyberspace.org% chmod a+x test grex.cyberspace.org% ls -l test -rwxrwx--x 1 gordybh cohorts 20 Dec 13 20:22 test grex.cyberspace.org% cat test #!/bin/sh echo test... (2 Replies)
Discussion started by: wmosley2
2 Replies
Login or Register to Ask a Question