Assistance on complicated perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assistance on complicated perl script
# 1  
Old 05-22-2017
Assistance on complicated perl script

As a followup to my previous thread, I'm trying to make a complicated perl script that involves storing information from a text file into a hash, and giving the user the ability to change the information present/write the information currently inside the hash to a new file. This is the code I've made for it but I'm running into a lot of issues getting it to work so I could use some assistance-

Code:
use warnings;
use strict;
use Scalar::Util 'looks_like_number';
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
{
        my %patientinfo = do {
        open my $patients, '<', 'Patient.txt';
        map {
        my ($first, $second, @info) = split;
        "$first $second" => \@info;
        } <$patients>;
        };
        print "Would you like to change the information? (y/n): ";
        my $inp;
        $inp = <STDIN>;
        chomp $inp;
        if (($inp ne "y") && ($inp ne "n"))
        {
                print "Would you like to change the information? (y/n): ";
                my $inp;
                $inp = <STDIN>;
                chomp $inp;
        }
        elsif ($inp eq "y")
        {
                print "Give Patient's Name: ";
                my $inpname;
                $inpname = <STDIN>;
                chomp $inpname;
                if (exists $patientinfo{$inpname})
                {
                        print "Enter 1 to change the gender, 2 to change the DOB, or 3 to change the disease: ";
                        my $inpnum;
                        $inpnum = <STDIN>;
                        chomp $inpnum;
                        if (($inpnum != 1) && ($inpnum != 2) && ($inpnum != 3))
                        {
                                print "Enter 1 to change the gender, 2 to change the DOB, or 3 to change the disease: ";
                                my $inpnum;
                                $inpnum = <STDIN>;
                                chomp $inpnum;
                        }
                        elsif ($inpnum == 1)
                        {
                                print "Please enter (M/F): ";
                                my $gender;
                                $gender = <STDIN>;
                                chomp $gender;
                                if (($gender ne "M") && ($gender ne "F"))
                                {
                                        print "Please enter (M/F): "
                                        my $gender;
                                        $gender = <STDIN>;
                                        chomp $gender;
                                }
                                else
                                {
                                        $patientinfo[1] = $gender;
                                }
                        }
                        elsif ($inpnum == 2)
                        {
                                print "Please enter (xx/xx/xx): ";
                                my $dob;
                                $dob = <STDIN>;
                                chomp $dob;
                                $patientinfo[2] = $dob;
                        }
                        else ($inpnum == 3)
                        {
                                print "Please enter a disease: ";
                                my $disease;
                                $disease = <STDIN>;
                                chomp $disease;
                                if ($disease !~ /^-?0/ && looks_like_number($disease))
                                {
                                        print "Please enter a disease: ";
                                        my $disease;
                                        $disease = <STDIN>;
                                        chomp $disease;
                                }
                                else
                                {
                                        $patientinfo[3] = $disease;
                                }
                        }
                        print "Would you like to print patient information to a file? (n/y): ";
                        my $print;
                        $print = <STDIN>;
                        chomp $print;
                        if (($print ne "n") && ($print ne "y"))
                        {
                                print "Would you like to print patient information to a file? (n/y): ";
                                my $print;
                                $print = <STDIN>;
                                chomp $print;
}
                        elsif ($print eq "n")
                        {
                                exit;
                        }
                        else
                        {
                                print "Give Patient name: "
                                my $name;
                                $name = <STDIN>;
                                chomp $name;
                                if (exists $patientinfo{$name})
                                {
                                        open(my $patientinfo, '>', 'Info.txt');
                                        print "Name: $patientinfo[0]";
                                        print "Gender: $patieninfo[1]";
                                        print "DOB: $patientinfo[2]";
                                        print "Disease: $patientinfo[3]";
                                        close $patientinfo;
                                }
                                else
                                {
                                        print "Error, name not found.";
                                        exit;
                                }
                        }

                }
                else
                {
                        print "Error, name not found. Moving on to part 2.";
                        print "Would you like to print patient information to a file? (n/y): ";
                        my $print;
                        $print = <STDIN>;
                        chomp $print;
                        if (($print ne "n") && ($print ne "y"))
                        {
                                print "Would you like to print patient information to a file? (n/y): ";
                                my $print;
                                $print = <STDIN>;
                                chomp $print;
                        }
                        elsif ($print eq "n")
                        {
                                exit;
                        }
                        else
                        {
                                print "Give Patient name: "
                                my $name;
                                $name = <STDIN>;
                                chomp $name;
                                if (exists $patientinfo{$name})
                                {
                                        open(my $patientinfo, '>', 'Info.txt');
                                        print "Name: $patientinfo[0]";
                                        print "Gender: $patieninfo[1]";
                                        print "DOB: $patientinfo[2]";
                                        print "Disease: $patientinfo[3]";
                                        close $patientinfo;
                                }
                                else
                                {
                                        print "Error, name not found.";
                                        exit;
                                }
                        }
                }
        else
        {
                print "Would you like to print patient information to a file? (n/y): ";
                my $print;
                $print = <STDIN>;
                chomp $print;
                if (($print ne "n") && ($print ne "y"))
                {
                        print "Would you like to print patient information to a file? (n/y): ";
                        my $print;
                        $print = <STDIN>;
                        chomp $print;
                }
                elsif ($print eq "n")
                {
                        exit;
                }
                else
                {
                        print "Give Patient name: "
                        my $name;
                        $name = <STDIN>;
                        chomp $name;
                        if (exists $patientinfo{$name})
                        {
                                open(my $patientinfo, '>', 'Info.txt');
                                print "Name: $patientinfo[0]";
                                print "Gender: $patieninfo[1]";
                                print "DOB: $patientinfo[2]";
                                print "Disease: $patientinfo[3]";
                                close $patientinfo;
                        }
                        else
                        {
                                print "Error, name not found.";
                                exit;
                        }
                }
        }
}

Basically after the initial setup asking the user if they want to proceed with the program, I take the data inside a file called Patient.txt and store it in a hash with the first and last name making up the key while the rest of the columns make up the values. Then the program asks if the user wants to change the information. If they say yes then they are asked for a patient name, and if it doesn't exist it moves on to the second part. If it does it'll give the user the option to change values in the hash. After this it would move on to the second part where it asks to write the info of one patient to a new file called Info.txt. If the patient doesn't exist it'll display an error message and exit. The user would also reach this second part if the patient name they entered for part 1 didn't exist, or if they chose not to change anything in the hash at all.

Once agian, here is the info stored in Patient.txt (note that none of these people actually exist)-
Code:
Celena Standard  F 01/24/94 Cancer 
Jeniffer Orlowski  F 06/24/86 None
Brent Koehler  M 12/05/97  HIV
Mao Schleich  M 04/17/60  Cancer
Goldie Moultrie  F 04/05/96  None
Silva Rizzo  F 10/26/78  Amyloidosis
Leatha Papenfuss  F 10/15/97  CREST
Vita Sabb  F 05/28/87  Autism
Alyce Ugarte  F 12/21/64  HIV
Ela Prout  F 12/05/57  Autism
Mohamed Buchannon  M 07/24/91  Caner
Lael Stall  M 12/05/97  None

Any help and guidance is greatly appreciated, and if there is a question about my code I'll try and answer it to the best of my ability.
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) when displaying full-line and multi-line sample input, output, and code segments.

Last edited by Don Cragun; 05-22-2017 at 02:09 AM.. Reason: Change ICODE tags to CODE tags again.
# 2  
Old 05-22-2017
You have shown us your code. You have stated a general outline of how your code is supposed to work. What is your code doing that you don't want it to do? What isn't your code doing that you do want it to do?

Are you getting error messages when you run your code? If so, exactly what diagnostics are you seeing?

What outputs are you getting?

What outputs do you want to get?
# 3  
Old 05-22-2017
First thing I'll note is that the very first line I submitted as my code is actually line 8 (due to comments I made on previous lines).

These are the error messages I'm receiving on my terminal-

Code:
syntax error at ./patient.pl line 73, near "my "
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 79.
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 88.
syntax error at ./patient.pl line 90, near "else ("
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 105.
syntax error at ./patient.pl line 126, near "my "
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at ./patient.pl line 126.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at ./patient.pl line 127.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at ./patient.pl line 128.
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at ./patient.pl line 129.
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 132.
Global symbol "@patieninfo" requires explicit package name (did you forget to declare "my @patieninfo"?) at ./patient.pl line 133.
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 134.
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 135.
syntax error at ./patient.pl line 167, near "my "

What I basically want as an output is for the program to run, and for there to be a new file in the directory called Info.txt with information inside it specific to how the user interacted with the program. For example, if the user changed the info for Celena Standard from F to M (in terms of the value for gender) and designated that name to be written into Info.txt, then that file should appear in the directory with this inside it-

Code:
Name: Celena Standard
Gender: M
DOB: 01/24/94
Disease: Cancer

Or on the flip side, say they didn't change the information and still chose that name (or they changed the info for one name but didn't choose that same person to write the info to Info.txt for the second part) then Info.txt would look identical to above but with the same values present in the Patient.txt file I gave above.
Moderator's Comments:
Mod Comment I repeat: Please use CODE tags (not ICODE tags) when displaying full-line and multi-line sample input, output, and code segments.

Last edited by Don Cragun; 05-22-2017 at 05:27 PM..
# 4  
Old 05-24-2017
I haven't gone through your entire program or understood the logic, but some of the messages seem to be due to standard beginner errors.

Code:
syntax error at ./patient.pl line 73, near "my "

Check the line right above the "my " on line 73. You are probably missing a semi-colon. All Perl executable statements end with a semi-colon.

Code:
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 79.
Global symbol "@patientinfo" requires explicit package name (did you forget to declare "my @patientinfo"?) at ./patient.pl line 88.

You have declared "patientinfo" as a hash i.e. "%patientinfo", but are using it as a list/array i.e. "$patientinfo[1] = $gender;"
Check the Perl documentation for the differences between lists and hashes and decide what you want to do.

Code:
syntax error at ./patient.pl line 90, near "else ("

"else" cannot be followed by a condition i.e. "else ($inpnum == 3)" is a syntax error. The fact that you are using an "else" branch means that you have exhausted all your conditions and want a catch-all provision at the end. If you do want to test a condition, then use "elsif" instead. But do understand it is not the same as "else".

Code:
Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?) at ./patient.pl line 126.

This one is interesting because although you have done a strict declaration of $name i.e. "my $name", Perl sees "my" as continuation of the previous line. That's because the previous line does not end with a semi-colon.
Again, all Perl executable statements end with a semi-colon.

Code:
Global symbol "@patieninfo" requires explicit package name (did you forget to declare "my @patieninfo"?) at ./patient.pl line 133.

Check the spelling: "patieninfo" is not the same as "patientinfo". Thereafter, decide what you want to use - the hash or the list. Or if you want to declare a separate list etc.

Use the suggestions above to fix the rest of the errors. I haven't gone through your program thoroughly, but I hope the above pointers help you in at least running the program successfully.
This User Gave Thanks to durden_tyler For This Post:
# 5  
Old 05-24-2017
I haven't gone through your script in detail either, but I did quickly find at least four print statements in your code that are not properly terminated as suggested by durden_tyler. If you fix those, you'll get considerably fewer errors.
# 6  
Old 05-25-2017
Okay so an update (sorry if this is late). I managed to get the program to run, but I'm not getting my desired output. When I get to part 2 without changing any information, and I input the name say, Celena Standard, I get these messages-

Code:
Use of uninitialized value $patientinfo[1] in concatenation (.) or string at ./patient.pl line 209, <STDIN> line 4.
Use of uninitialized value $patientinfo[2] in concatenation (.) or string at ./patient.pl line 210, <STDIN> line 4.
Use of uninitialized value $patientinfo[3] in concatenation (.) or string at ./patient.pl line 211, <STDIN> line 4.

The Info.txt file appears in my directory but with nothing in it at all too.

Also for some reason my error checking at the very beginning isn't working properly. If the user inputs a 'c' or 'q' it works fine but if they say, enter '3' they get prompted again and if they enter an incorrect input again the program exits even if they didn't hit 'q' (it even exits if they hit 'c' when it should continue the program with this input). I am really confused as to why I'm running into this problem because I set up the beginning of an interactive Perl script in the past with those same lines of code and it worked just fine.

EDIT: I've fixed the issue with the beginning of my program

Last edited by Eric1; 05-25-2017 at 11:27 PM..
# 7  
Old 05-26-2017
Quote:
Originally Posted by Eric1
...
When I get to part 2 without changing any information, and I input the name say, Celena Standard, I get these messages-

Code:
Use of uninitialized value $patientinfo[1] in concatenation (.) or string at ./patient.pl line 209, <STDIN> line 4.
Use of uninitialized value $patientinfo[2] in concatenation (.) or string at ./patient.pl line 210, <STDIN> line 4.
Use of uninitialized value $patientinfo[3] in concatenation (.) or string at ./patient.pl line 211, <STDIN> line 4.

The Info.txt file appears in my directory but with nothing in it at all too.
...
Since it is printing "$patientinfo[1]" in the error message, my hunch is that you have now added a declaration of the array "patientinfo".
However, that array never got populated because you have a hash "patientinfo" near the beginning of your program. Since the array is uninitialized, Perl prints that message.

The file Info.txt has nothing in it because the lines that were supposed to write in that file errored out as seen above. The open() function created an empty file and the close() function closed that empty file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on a fairly complicated shell script

Hello, also with the help of some great users of this forum, I have created following shell script. MM=120 GG=5000 # get size of directory szm=$(du -s --block-size M ./192.168.1.xxx | awk '{print int($0)}') data=$(date --rfc-3339=seconds) if ; then # too big delete older files ... (10 Replies)
Discussion started by: dcaccount
10 Replies

2. Shell Programming and Scripting

awk, sed, perl assistance in outputting formatted file

Hello, Please advise. Scoured this site, as well as google for answers. However if you do not know what to search for, it's a bit hard to find answers. INPUT: ACTASS= 802 BASECOS= 279 COSNCHG= 3 CUSCOS= 52 UPLDCOS= 2 DESIRED OUTPUT: ACTASS=802 BASECOS=279 (13 Replies)
Discussion started by: abacus
13 Replies

3. Shell Programming and Scripting

Script Search replace - complicated

I have a text file for which i need a script which does some fancy search and replace. Basically i want to loop through each line, if i find an occurance of certain string format then i want to carry on search on replace another line, once i replaced this line i will contine to search for the... (7 Replies)
Discussion started by: kelseyh
7 Replies

4. Shell Programming and Scripting

Assistance in Perl scripting

PFA file "color.txt". Note : There is no newline character in the file. I have manually inserted the newline char to make it easy to understand. I am expecting out in the form as specified in second file "out.txt" I need a perl script to perform the task. Thanks in advance. (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

5. Shell Programming and Scripting

Assistance needed with perl script

Ok, theres a log file containing the below. Lets call the logfile log_fantastic: 2009/03/16 21:42:45 USER: tonnabo - MAC: 0014BF2D385A - STATUS_ID: 30 - STATE: ERROR 2009/03/16 21:42:45 USER: tonnabo - MAC: 001310AC120D - STATUS_ID: 15 - STATE: OK 2009/03/16 21:42:45 USER: tonnabo - MAC:... (5 Replies)
Discussion started by: SkySmart
5 Replies

6. Shell Programming and Scripting

In need of multi threaded perl assistance

I need to write a perl script to execute external programs and grab the output and return code. Each program should be killed if it has not completed within X seconds. Imagine that the script goes something like this : @commands = &get_commands(); foreach $cmd (@commands) { $pid =... (4 Replies)
Discussion started by: SandmanCL
4 Replies

7. Shell Programming and Scripting

need help with script - output to file, but complicated

ok, so what i want to do is make a script that will do the following: take out from a command in the terminal put that output into a text file already on my computer. the only thing is that i need to put the output in the file kinda weird: i need to take it and put each character of output... (13 Replies)
Discussion started by: twoodcc
13 Replies

8. Shell Programming and Scripting

Very complicated script..

I have a script that I need to create tha involves moving files and renaming them(see previous post) Are there any websites with user made shell scripts? (5 Replies)
Discussion started by: rocinante
5 Replies

9. Shell Programming and Scripting

Perl script assistance; paste word into external command

I'm attempting to create a Perl script that will: Take the contents of the usernames.tmp file (usernames.tmp is created from an awk one-liner ran against /etc/passwd) Take one line at a time and pass it to the su command as a users name. This should go on until there is no more name to... (10 Replies)
Discussion started by: bru
10 Replies

10. Shell Programming and Scripting

Assistance with Perl and HTTP

I need to query a http site and then parse the xml results, this works well if I use the string in IE but I require an automated solution. I have tried using the following as well as HTTP::Request, nothing seems to work any suggestions would be appreciated, I have tried diffrnt things I found on... (7 Replies)
Discussion started by: bryanthomas
7 Replies
Login or Register to Ask a Question