05-26-2017
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: "; #prompts the user to enter either q Q or c
my $input; #declares the variable input
$input = <STDIN>; #the user input will be stored in the variable called input
chomp $input; #the value in the variable called input will be chomped
while (($input ne "q") && ($input ne "Q") && ($input ne "c")) #this conditional is for checking if the user inputted anything other than q Q or 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: "; #if the user inputted anything other than q Q or c then they will be prompted again
$input = <STDIN>; #the new user input will be stored in the variable called input
chomp $input; #the new value in the variable called input will be chomped
}
if (($input eq "q") || ($input eq "Q")) #this conditional is for checking if the user inputed either q or Q
{
exit; #if they inputted either q or Q then the program will stop running
}
else
{
my %patientinfo = do {
open my $patientinfo, '<', 'Patient_Info.txt';
map {
my ($first, $second, @info) = split;
"$first $second" => \@info;
} <$patientinfo>;
}; #this is for creating the hash that stores the info inside the text file
my @patientinfo = <%patientinfo>; #I also store the info from the hash inside an array
print "Would you like to change the information? (y/n): "; #prompts the user
my $inp; #declares variable
$inp = <STDIN>; #takes the users input
chomp $inp; #chomps the users input
while (($inp ne "y") && ($inp ne "n")) #conditional loop to check and see if the users input doesn't equal y or n
{
print "Would you like to change the information? (y/n): "; #prompts the user again
my $inp; #declares variable again
$inp = <STDIN>; #takes the users input again
chomp $inp; #chomps the users input again
}
if ($inp eq "y") #checks to see if the users input equals y
{
print "Give Patient's Name: "; #prompts the user
my $inpname; #declares variable
$inpname = <STDIN>; #takes the users input
chomp $inpname; #chomps the users input
if (exists $patientinfo{$inpname}) #checks to see if patient name exists in hash
{
print "Enter 1 to change the gender, 2 to change the DOB, or 3 to change the disease: "; #prompts the user
my $inpnum; #declares variable
$inpnum = <STDIN>; #takes the users input
chomp $inpnum; #chomps the users input
while (($inpnum != 1) && ($inpnum != 2) && ($inpnum != 3)) #conditional loop to check and see if the users input doesn't equal to 1 2 or 3
{
print "Enter 1 to change the gender, 2 to change the DOB, or 3 to change the disease: "; #prompts the user
my $inpnum; #declares variable
$inpnum = <STDIN>; #takes the users input
chomp $inpnum; #chomps the users input
}
if ($inpnum == 1) #checks to see if user input equals to 1
{
print "Please enter (M/F): "; #prompts the user
my $gender; #declares variable
$gender = <STDIN>; #takes the users input
chomp $gender; #chomps the users input
while (($gender ne "M") && ($gender ne "F")) #checks to see if the user input doesn't equal to either M or F
{
print "Please enter (M/F): "; #prompts the user again
my $gender; #declares variable again
$gender = <STDIN>; #takes the users input again
chomp $gender; #chomps the users input again
}
if (($gender eq "M") || ($gender eq "F"))
{
$patientinfo[1] = $gender; #changes the value inside the array
}
else
{
;
}
}
elsif ($inpnum == 2) #checks to see if user input equals to 2
{
print "Please enter (xx/xx/xx): "; #prompts the user
my $dob; #declares variable
$dob = <STDIN>; #takes the users input
chomp $dob; #chomps the users input
$patientinfo[2] = $dob; #changes the value inside the array
}
else
{
print "Please enter a disease: "; #prompts the user
my $disease; #declares variable
$disease = <STDIN>; #takes the users input
chomp $disease; #chomps the users input
while ($disease !~ /^-?0/ && looks_like_number($disease)) #checks to see if the user input was a number
{
print "Please enter a disease: "; #prompts the user again
my $disease; #declares variable again
$disease = <STDIN>; #takes the users input again
chomp $disease; #chomps the users input again
}
$patientinfo[3] = $disease; #changes the value insude the array
}
}
print "Would you like to print patient information to a file? (n/y): "; #prompts the user
my $print; #declares variable
$print = <STDIN>; #takes the users input
chomp $print; #chomps the users input
while (($print ne "n") && ($print ne "y")) #checks to see if user input doesn't equal to either n or y
{
print "Would you like to print patient information to a file? (n/y): "; #prompts the user
my $print; #declares variable
$print = <STDIN>; #takes the users input
chomp $print; #chomps the users input
}
if ($print eq "n") #checks to see if user input equals n
{
exit; #exits the script
}
else
{
print "Give Patient name: "; #prompts the user
my $names1 = <STDIN>; #declares a variable and takes the users input
chomp $names1; #chomps the users input
if (exists $patientinfo{$names1}) #checks to see if the users input exists in the hash
{
open(my $patientinfo, '>', 'Info_Accessed.txt');
print "Name: $patientinfo[0]";
print "Gender: $patientinfo[1]";
print "DOB: $patientinfo[2]";
print "Disease: $patientinfo[3]";
close $patientinfo; #writes all the info to a new text file called Info_Accessed.txt
}
else
{
print "Error, name not found."; #displays an error message for the user
exit; #exits the script
}
}
}
else
{
print "Error, name not found. Moving on to part 2."; #displays an error message for the user
print "Would you like to print patient information to a file? (n/y): "; #prompts the user
my $print1; #declare a variable
$print1 = <STDIN>; #takes the users input
chomp $print1; #chomps the users input
while (($print1 ne "n") && ($print1 ne "y")) #checks to see if the users input doesn't equal to either n or y
{
print "Would you like to print patient information to a file? (n/y): "; #prompts the user
my $print1; #declares variable
$print1 = <STDIN>; #takes the users input
chomp $print1; #chomps the users input
}
if ($print1 eq "n") #checks to see if the users input equals n
{
exit; #exits the script
}
else
{
print "Give Patient name: "; #prompts the user
my $na = <STDIN>; #declares a variable and takes the users input
chomp $na; #chomps the users input
if (exists $patientinfo{$na}) #checks to see if the users input exists in the hash
{
open(my $patientinfo, '>', 'Info_Accessed.txt');
print "Name: $patientinfo[0]";
print "Gender: $patientinfo[1]";
print "DOB: $patientinfo[2]";
print "Disease: $patientinfo[3]";
close $patientinfo; #writes all the info to a new text file called Info_Accessed.txt
}
else
{
print "Error, name not found."; #displays an error message for the user
exit; #exits the script
}
}
}
***else***
{
print "Would you like to print patient information to a file? (n/y): "; #prompts the user
my $print; #declares variable
$print = <STDIN>; #tales the users input
chomp $print; #chomps the users input
while (($print ne "n") && ($print ne "y")) #checks to see if the users input doesn't equal n and y
{
print "Would you like to print patient information to a file? (n/y): "; #prompts the user again
my $print; #declares variable again
$print = <STDIN>; #takes the users input again
chomp $print; #chomps the users input again
}
if ($print eq "n") #checks to see if the user input equals n
{
exit; #exits the script
}
else
{
print "Give Patient name: "; #prompts the user
my $na1 = <STDIN>; #declares a variable and takes the users input
chomp $na1; #chomps the users input
if (exists $patientinfo{$na1}) #checks to see if the users input exists in the hash
{
open(my $patientinfo, '>', 'Info_Accessed.txt');
print "Name: $patientinfo[0]";
print "Gender: $patientinfo[1]";
print "DOB: $patientinfo[2]";
print "Disease: $patientinfo[3]";
close $patientinfo; #writes all the info to a new text file called Info_Accessed.txt
}
else
{
print "Error, name not found."; #displays an error message for the user
exit; #exits the script
}
}
}
***}***
I can't run my program anymore because there are syntax errors at the parts of my code towards the end I highlighted with asterisks. Any idea why?
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. Shell Programming and Scripting
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
3. Shell Programming and Scripting
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
4. Shell Programming and Scripting
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
5. Shell Programming and Scripting
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
6. Shell Programming and Scripting
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
7. Shell Programming and 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
8. Shell Programming and Scripting
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
9. Shell Programming and Scripting
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
10. Shell Programming and Scripting
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