220 likes | 331 Views
IF statements - selection. Please use speaker notes for additional information!. The divider is ,space. Record shown being processed on previous slide. <!payroll.html> <html> <head><title>Mailing List</title></head> <body> <h1>Mailing List</h1>
E N D
IF statements - selection Please use speaker notes for additional information!
The divider is ,space. Record shown being processed on previous slide.
<!payroll.html> <html> <head><title>Mailing List</title></head> <body> <h1>Mailing List</h1> <form action="http://www.pgrocer.com/cgi-bin/data/payroll.cgi" method=post> Enter Name:<br> <input type=text name=name size=25><br><br> Enter Address:<br> <input type=text name=stadr size=25><br><br> Enter City:<br> <input type=text name=city size=20><br><br> Enter State:<br> <input type=text name=state size=2<br><br> Enter ZIP: <input type=text name=zip size=10<br><br> Enter Job Type (S for Salaried, F for Full Time Hourly, P for Part Time Hourly): <input type=text name=jobtype size=2><br><br> Enter Job Code: <input type=text name=jobcode size=2><br><br> Enter Pay Per Hour: <input type=text name=payperhr size=10> or Enter Salary: <input type=text name=salary size=12><br><br> <input type=submit value="Submit"> <input type=reset value="Reset"> </form> </body> </html>
#!/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 "<html><head><title>Record just entered</title><basefont size=5></head>\n"; print "<body>\n"; print "<div align=center>\n"; print "$name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary\n"; print "</div></body></html>\n"; Code to write records to sequential file.
#!/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(<INF>) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/,/,$_); print "$name<br>\n"; } close(INF); print "</body></html>\n"; The while loop in this case is set up to handle all the records in the file. This is coded with while(<INF>) where INF is the name I used to open and close the file. The code in the while loop is enclosed with curly brackets.
#!/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 "<html><head><title>Salaried Workers</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Salaried Workers:</h1>\n"; while(<INF>) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { print "$name - Salaried - $jobtype<br>\n"; } } close(INF); print "</body></html>\n"; Note that the split divider is given as ,space.
#!/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 "<html><head><title>Salaried Workers</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Salaried Workers:</h1>\n"; while(<INF>) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { print "$name - Salaried - $jobtype<br>\n"; } else { print "$name - Hourly - $jobtype<br>\n"; } } close(INF); print "</body></html>\n";
#!/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 "<html><head><title>Salaried Workers</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Salaried Workers:</h1>\n"; while(<INF>) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobcode == 1 ) { print "$name - Job Type #1 - $jobcode<br>\n"; } else { print "$name - All Other Job Types - $jobcode<br>\n"; } } close(INF); print "</body></html>\n";
if ($jobcode == 1 ) { print "$name - Job Type #1 - $jobcode<br>\n"; } else { print "$name - All Other Job Types - $jobcode<br>\n"; }
#!/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 "<html><head><title>Employees</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Employees:</h1>\n"; while(<INF>) { 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<br>\n"; } else { print "$name - $salary - Salary not greater than or equal to 50000<br>\n"; } } else { print "$name - $payperhr - Hourly employee<br>\n"; } } close(INF); print "</body></html>\n";
jobtype eq “S” N Y salary >= 50000 Hourly employee N Y Salary not > 50000 Salary > 50000 if ($jobtype eq "S") { if ($salary >= 50000) { print "$name - $salary - Salary greater than 50000<br>\n"; } else { print "$name - $salary - Salary not greater than or equal to 50000<br>\n"; } } else { print "$name - $payperhr - Hourly employee<br>\n"; }
#!/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 "<html><head><title>Employees</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Employees:</h1>\n"; while(<INF>) { 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<br>\n"; } else { print "$name - other employees<br>\n"; } } close(INF); print "</body></html>\n";
jobtype eq “S” N Y salary >= 50000 Other employee N Y Other employee Salary > 50000 if ($jobtype eq "S" and $salary >= 50000) { print "$name - $salary - Salary greater than 50000<br>\n"; } else { print "$name - other employees<br>\n"; }
#!/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 "<html><head><title>Employees</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Employees:</h1>\n"; while(<INF>) { chomp($_); ($name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary) = split(/, /,$_); if ($jobtype eq "S") { print "$name - $jobtype - $jobcode - jobtype = S<br>\n"; } else { if ($jobcode == 1) { print "$name - $jobtype - $jobcode - jobcode = 1<br>\n"; } else { print "$name - other employees<br>\n"; } } } close(INF); print "</body></html>\n";
jobtype eq “S” N Y jobtype=S jobcode= 1 N Y Other employee jobcode= 1 if ($jobtype eq "S") { print "$name - $jobtype - $jobcode - jobtype = S<br>\n"; } else { if ($jobcode == 1) { print "$name - $jobtype - $jobcode - jobcode = 1<br>\n"; } else { print "$name - other employees<br>\n"; } }
#!/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 "<html><head><title>Employees</title><basefont size=5></head>\n"; print "<body>\n"; print "<h1>Employees:</h1>\n"; while(<INF>) { 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<br>\n"; } else { print "$name - other employees<br>\n"; } } close(INF); print "</body></html>\n";
jobtype eq “S” N Y Either S or 1 jobcode= 1 N Y Other employee Either S or 1 if ($jobtype eq "S" or $jobcode == 1) { print "$name - $jobtype - $jobcode - either S or 1<br>\n"; } else { print "$name - other employees<br>\n"; }