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-
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)-
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.