Assistance on complicated perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assistance on complicated perl script
# 8  
Old 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?
# 9  
Old 05-26-2017
That's because it is an "orphan" else branch.
It does not have a corresponding if-elsif branch.
I have added line numbers to your code and added color coding to make it easier to spot the if/elsif/else branches.
The closing brace at line 217 is for the opening brace at line 19.

Code:
 $ cat -n patient.pl
     1  use warnings;
     2  use strict;
     3  use Scalar::Util 'looks_like_number';
     4  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
     5  my $input; #declares the variable input
     6  $input = <STDIN>; #the user input will be stored in the variable called input
     7  chomp $input; #the value in the variable called input will be chomped
     8  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
     9  {
    10          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
    11          $input = <STDIN>; #the new user input will be stored in the variable called input
    12          chomp $input; #the new value in the variable called input will be chomped
    13  }
   14  if (($input eq "q") || ($input eq "Q")) #this conditional is for checking if the user inputed either q or Q
    15  {
   16          exit; #if they inputted either q or Q then the program will stop running
   17  }
   18  else
    19  {
   20          my %patientinfo = do {
    21          open my $patientinfo, '<', 'Patient_Info.txt';
    22          map {
    23          my ($first, $second, @info) = split;
    24          "$first $second" => \@info;
    25          } <$patientinfo>;
    26          }; #this is for creating the hash that stores the info inside the text file
    27          my @patientinfo = <%patientinfo>; #I also store the info from the hash inside an array
    28          print "Would you like to change the information? (y/n): "; #prompts the user
    29          my $inp; #declares variable
    30          $inp = <STDIN>; #takes the users input
    31          chomp $inp; #chomps the users input
    32          while (($inp ne "y") && ($inp ne "n")) #conditional loop to check and see if the users input doesn't equal y or n
    33          {
    34                  print "Would you like to change the information? (y/n): "; #prompts the user again
    35                  my $inp; #declares variable again
    36                  $inp = <STDIN>; #takes the users input again
    37                  chomp $inp;  #chomps the users input again
    38          }
   39          if ($inp eq "y") #checks to see if the users input equals y
    40          {
   41                  print "Give Patient's Name: "; #prompts the user
    42                  my $inpname; #declares variable
    43                  $inpname = <STDIN>; #takes the users input
    44                  chomp $inpname; #chomps the users input
    45                  if (exists $patientinfo{$inpname}) #checks to see if patient name exists in hash
    46                  {
    47                          print "Enter 1 to change the gender, 2 to change the DOB, or 3 to change the disease: "; #prompts the user
    48                          my $inpnum; #declares variable
    49                          $inpnum = <STDIN>; #takes the users input
    50                          chomp $inpnum; #chomps the users input
    51                          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
    52                          {
    53                                  print "Enter 1 to change the gender, 2 to change the DOB, or 3 to change the disease: "; #prompts the user
    54                                  my $inpnum; #declares variable
    55                                  $inpnum = <STDIN>; #takes the users input
    56                                  chomp $inpnum; #chomps the users input
    57                          }
    58                          if ($inpnum == 1) #checks to see if user input equals to 1
    59                          {
    60                                  print "Please enter (M/F): "; #prompts the user
    61                                  my $gender; #declares variable
    62                                  $gender = <STDIN>; #takes the users input
    63                                  chomp $gender; #chomps the users input
    64                                  while (($gender ne "M") && ($gender ne "F")) #checks to see if the user input doesn't equal to either M or F
    65                                  {
    66                                          print "Please enter (M/F): "; #prompts the user again
    67                                          my $gender; #declares variable again
    68                                          $gender = <STDIN>; #takes the users input again
    69                                          chomp $gender; #chomps the users input again
    70                                  }
    71                                  if (($gender eq "M") || ($gender eq "F"))
    72                                  {
    73                                          $patientinfo[1] = $gender; #changes the value inside the array
    74                                  }
    75                                  else
    76                                  {
    77                                          ;
    78                                  }
    79                          }
    80                          elsif ($inpnum == 2) #checks to see if user input equals to 2
    81                          {
    82                                  print "Please enter (xx/xx/xx): "; #prompts the user
    83                                  my $dob; #declares variable
    84                                  $dob = <STDIN>; #takes the users input
    85                                  chomp $dob; #chomps the users input
    86                                  $patientinfo[2] = $dob; #changes the value inside the array
    87                          }
    88                          else
    89                          {
    90                                  print "Please enter a disease: "; #prompts the user
    91                                  my $disease; #declares variable
    92                                  $disease = <STDIN>; #takes the users input
    93                                  chomp $disease; #chomps the users input
    94                                  while ($disease !~ /^-?0/ && looks_like_number($disease)) #checks to see if the user input was a number
    95                                  {
    96                                          print "Please enter a disease: "; #prompts the user again
    97                                          my $disease; #declares variable again
    98                                          $disease = <STDIN>; #takes the users input again
    99                                          chomp $disease; #chomps the users input again
   100                                  }
   101                                          $patientinfo[3] = $disease; #changes the value insude the array
   102                                  }
   103                          }
   104                          print "Would you like to print patient information to a file? (n/y): "; #prompts the user
   105                          my $print; #declares variable
   106                          $print = <STDIN>; #takes the users input
   107                          chomp $print; #chomps the users input
   108                          while (($print ne "n") && ($print ne "y")) #checks to see if user input doesn't equal to either n or y
   109                          {
   110                                  print "Would you like to print patient information to a file? (n/y): "; #prompts the user
   111                                  my $print; #declares variable
   112                                  $print = <STDIN>; #takes the users input
   113                                  chomp $print; #chomps the users input
   114                          }
   115                          if ($print eq "n") #checks to see if user input equals n
   116                          {
   117                                  exit; #exits the script
   118                          }
   119                          else
   120                          {
   121                                  print "Give Patient name: "; #prompts the user
   122                                  my $names1 = <STDIN>; #declares a variable and takes the users input
   123                                  chomp $names1; #chomps the users input
   124                                  if (exists $patientinfo{$names1}) #checks to see if the users input exists in the hash
   125                                  {
   126                                          open(my $patientinfo, '>', 'Info_Accessed.txt');
   127                                          print "Name: $patientinfo[0]";
   128                                          print "Gender: $patientinfo[1]";
   129                                          print "DOB: $patientinfo[2]";
   130                                          print "Disease: $patientinfo[3]";
   131                                          close $patientinfo; #writes all the info to a new text file called Info_Accessed.txt
   132                                  }
   133                                  else
   134                                  {
   135                                          print "Error, name not found."; #displays an error message for the user
   136                                          exit; #exits the script
   137                                  }
   138                          }
  139                  }
  140                  else
   141                  {
  142                          print "Error, name not found. Moving on to part 2."; #displays an error message for the user
   143                          print "Would you like to print patient information to a file? (n/y): "; #prompts the user
   144                          my $print1; #declare a variable
   145                          $print1 = <STDIN>; #takes the users input
   146                          chomp $print1; #chomps the users input
   147                          while (($print1 ne "n") && ($print1 ne "y")) #checks to see if the users input doesn't equal to either n or y
   148                          {
   149                                  print "Would you like to print patient information to a file? (n/y): "; #prompts the user
   150                                  my $print1; #declares variable
   151                                  $print1 = <STDIN>; #takes the users input
   152                                  chomp $print1; #chomps the users input
   153                          }
   154                          if ($print1 eq "n") #checks to see if the users input equals n
   155                          {
   156                                  exit; #exits the script
   157                          }
   158                          else
   159                          {
   160                                  print "Give Patient name: "; #prompts the user
   161                                  my $na = <STDIN>; #declares a variable and takes the users input
   162                                  chomp $na; #chomps the users input
   163                                  if (exists $patientinfo{$na}) #checks to see if the users input exists in the hash
   164                                  {
   165                                          open(my $patientinfo, '>', 'Info_Accessed.txt');
   166                                          print "Name: $patientinfo[0]";
   167                                          print "Gender: $patientinfo[1]";
   168                                          print "DOB: $patientinfo[2]";
   169                                          print "Disease: $patientinfo[3]";
   170                                          close $patientinfo; #writes all the info to a new text file called Info_Accessed.txt
   171                                  }
   172                                  else
   173                                  {
   174                                          print "Error, name not found."; #displays an error message for the user
   175                                          exit; #exits the script
   176                                  }
   177                          }
  178                  }
  179          ***else***
   180          {
   181                  print "Would you like to print patient information to a file? (n/y): "; #prompts the user
   182                  my $print; #declares variable
   183                  $print = <STDIN>; #tales the users input
   184                  chomp $print; #chomps the users input
   185                  while (($print ne "n") && ($print ne "y")) #checks to see if the users input doesn't equal n and y
   186                  {
   187                          print "Would you like to print patient information to a file? (n/y): "; #prompts the user again
   188                          my $print; #declares variable again
   189                          $print = <STDIN>; #takes the users input again
   190                          chomp $print; #chomps the users input again
   191                  }
   192                  if ($print eq "n") #checks to see if the user input equals n
   193                  {
   194                          exit; #exits the script
   195                  }
   196                  else
   197                  {
   198                          print "Give Patient name: "; #prompts the user
   199                          my $na1 = <STDIN>; #declares a variable and takes the users input
   200                          chomp $na1; #chomps the users input
   201                          if (exists $patientinfo{$na1}) #checks to see if the users input exists in the hash
   202                          {
   203                                  open(my $patientinfo, '>', 'Info_Accessed.txt');
   204                                  print "Name: $patientinfo[0]";
   205                                  print "Gender: $patientinfo[1]";
   206                                  print "DOB: $patientinfo[2]";
   207                                  print "Disease: $patientinfo[3]";
   208                                  close $patientinfo; #writes all the info to a new text file called Info_Accessed.txt
   209                          }
   210                          else
   211                          {
   212                                  print "Error, name not found."; #displays an error message for the user
   213                                  exit; #exits the script
   214                          }
   215                  }
   216          }
  217  ***}***
   218
 $
$

A few points to remember about if/elsif/else branch:
1) If you're going to use this branch, then there must be exactly 1 "if" branch. It must be at the beginning. It must be followed by a condition. You cannot have more than one "if" branch in the same "if/elsif/else" branch.
2) Thereafter, there can be 0, 1 or more "elsif" branch(es). They must be after the "if" branch. They must be followed by a condition i.e. any expression or string that evaluates to true or false.
3) Thereafter, there can be 0 or 1 "else" branch. They do not check any condition. You cannot have more than 1 "else" branch. It must be at the end, after the "if" or "elsif" branches.

---------- Post updated at 11:10 AM ---------- Previous update was at 10:59 AM ----------

By the way, if you use editors like vim, gvim, emacs etc. then they have keyboard shortcuts that allow you to "jump" between opening/closing braces.
If you use GUI editors on Windows or Linux, then many of them show vertical lines on the left that span entire "if", "elsif", "else" brances.
In some of them, you could even click on small icons on the left to expand or collapse these brances.
These are small things, but they make coding much easier.
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