Presentation is loading. Please wait.

Presentation is loading. Please wait.

IF statements - selection Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "IF statements - selection Please use speaker notes for additional information!"ā€” Presentation transcript:

1 IF statements - selection Please use speaker notes for additional information!

2

3 Record shown being processed on previous slide. The divider is,space.

4 Mailing List Mailing List Enter Name: Enter Address: Enter City: Enter State: Enter ZIP: Enter Job Type (S for Salaried, F for Full Time Hourly, P for Part Time Hourly): Enter Job Code: Enter Pay Per Hour: or Enter Salary:

5 #!/usr/bin/perl #payroll.cgi - saves name and address information to make a mailing list use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); #assign variables $name = param('name'); $stadr = param('stadr'); $city = param('city'); $state = param('state'); $zip = param('zip'); $jobtype = param('jobtype'); $jobcode = param('jobcode'); $payperhr = param('payperhr'); $salary = param('salary'); #save form data to a file open(FILEOUT, ">>", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print FILEOUT "$name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary\n"; close(FILEOUT); print " Record just entered \n"; print " \n"; print "$name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary\n"; print " \n"; Code to write records to sequential file.

6 #!/usr/bin/perl #payrollrd.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/,/,$_); print "$name \n"; } close(INF); print " \n"; The while loop in this case is set up to handle all the records in the file. This is coded with while( ) where INF is the name I used to open and close the file. The code in the while loop is enclosed with curly brackets.

7 #!/usr/bin/perl #payrollsal.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Salaried Workers \n"; print " \n"; print " Salaried Workers: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { print "$name - Salaried - $jobtype \n"; } close(INF); print " \n"; Note that the split divider is given as,space.

8 #!/usr/bin/perl #payrollsal1.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Salaried Workers \n"; print " \n"; print " Salaried Workers: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { print "$name - Salaried - $jobtype \n"; } else { print "$name - Hourly - $jobtype \n"; } } close(INF); print " \n";

9 #!/usr/bin/perl #payrolljobcd.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Salaried Workers \n"; print " \n"; print " Salaried Workers: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobcode == 1 ) { print "$name - Job Type #1 - $jobcode \n"; } else { print "$name - All Other Job Types - $jobcode \n"; } } close(INF); print " \n";

10 if ($jobcode == 1 ) { print "$name - Job Type #1 - $jobcode \n"; } else { print "$name - All Other Job Types - $jobcode \n"; }

11 #!/usr/bin/perl #payrollsalamt.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Employees \n"; print " \n"; print " Employees: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { if ($salary >= 50000) { print "$name - $salary - Salary greater than 50000 \n"; } else { print "$name - $salary - Salary not greater than or equal to 50000 \n"; } else { print "$name - $payperhr - Hourly employee \n"; } close(INF); print " \n";

12 jobtype eq ā€œSā€ salary >= 50000 Salary > 50000 Salary not > 50000 Hourly employee Y Y N N if ($jobtype eq "S") { if ($salary >= 50000) { print "$name - $salary - Salary greater than 50000 \n"; } else { print "$name - $salary - Salary not greater than or equal to 50000 \n"; } else { print "$name - $payperhr - Hourly employee \n"; }

13

14 #!/usr/bin/perl #payrollcom.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Employees \n"; print " \n"; print " Employees: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S" and $salary >= 50000) { print "$name - $salary - Salary greater than 50000 \n"; } else { print "$name - other employees \n"; } close(INF); print " \n";

15 if ($jobtype eq "S" and $salary >= 50000) { print "$name - $salary - Salary greater than 50000 \n"; } else { print "$name - other employees \n"; } jobtype eq ā€œSā€ salary >= 50000 Salary > 50000 Other employee Other employee Y Y N N

16

17 #!/usr/bin/perl #payrollcomor.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Employees \n"; print " \n"; print " Employees: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { print "$name - $jobtype - $jobcode - jobtype = S \n"; } else { if ($jobcode == 1) { print "$name - $jobtype - $jobcode - jobcode = 1 \n"; } else { print "$name - other employees \n"; } close(INF); print " \n";

18 if ($jobtype eq "S") { print "$name - $jobtype - $jobcode - jobtype = S \n"; } else { if ($jobcode == 1) { print "$name - $jobtype - $jobcode - jobcode = 1 \n"; } else { print "$name - other employees \n"; } jobtype eq ā€œSā€ jobcode= 1 Other employee Y Y N N jobtype=S

19

20 #!/usr/bin/perl #payrollcomor.cgi - reads the payroll file use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll.txt") or die "file error: payroll.txt. $!, stopped"; print " Employees \n"; print " \n"; print " Employees: \n"; while( ) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S" or $jobcode == 1) { print "$name - $jobtype - $jobcode - either S or 1 \n"; } else { print "$name - other employees \n"; } close(INF); print " \n";

21 if ($jobtype eq "S" or $jobcode == 1) { print "$name - $jobtype - $jobcode - either S or 1 \n"; } else { print "$name - other employees \n"; } jobtype eq ā€œSā€ jobcode= 1 Either S or 1 Other employee Y Y N N Either S or 1

22


Download ppt "IF statements - selection Please use speaker notes for additional information!"

Similar presentations


Ads by Google