Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Functions Please use speaker notes for additional information!"— Presentation transcript:

1 Functions Please use speaker notes for additional information!

2 Converter Converter: Inches and Centimeters Using this converter you can convert centimeters into inches and inches into centimeters. The formula I am using to convert centimeters to inches is multiply the cm by.39. The formula I am using to convert inches to centimeters is multiply in by 2.54. Enter the type of conversion: Centimeters to Inches Inches to Centimeters Enter the number to convert:

3 #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use strict; my ($num, $type, $ans); $num = param('num'); $type = param('type'); print " \n"; print " Convert \n"; if ($type eq "cm") { toinches(); } elsif ($type eq "in") { tocm(); } else { print "No selection was made - try again! \n"; } print " Click here to return to converter \n"; print " \n"; exit; # ***** user defined functions **** The user defined functions are shown on the next slide. The exit; statement is used to terminate the code. It is not required in Perl. Depending on the result of the if comparison, different functions are called. The functions are shown on the next slide.

4 sub toinches { $ans = ($num *.39); print "$num centimeters is equal to $ans inches \n"; } sub tocm { $ans = ($num * 2.54); print "$num inches is equal to $ans centimeters \n"; }

5 #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use strict; my ($num, $type, $ans, $from, $to); $num = param('num'); $type = param('type'); print " \n"; print " Convert \n"; if ($type eq "cm") { toinches(); } elsif ($type eq "in") { tocm(); } else { print "No selection was made - try again! \n"; } print "$num $from is equal to $ans $to \n"; print " Click here to return to converter \n"; print " \n"; exit; # ***** user defined functions **** sub toinches { $ans = ($num *.39); $from = "centimeters"; $to = "inches"; return $ans, $from, $to; } sub tocm { $ans = ($num * 2.54); $from = "inches"; $to = "centimeters"; return $ans, $from, $to; } Continued on the top of the next column.

6 #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); use strict; my ($num, $type); $num = param('num'); $type = param('type'); print " \n"; print " Convert \n"; if ($type eq "cm") { toinches(); } elsif ($type eq "in") { tocm(); } else { print "No selection was made - try again! \n"; } print "$num $from is equal to $ans $to \n"; print " Click here to return to converter \n"; print " \n"; exit; # ***** user defined functions **** sub toinches { my ($ans, $from, $to); $ans = ($num *.39); $from = "centimeters"; $to = "inches"; return $ans, $from, $to; } sub tocm { my ($ans, $from, $to); $ans = ($num * 2.54); $from = "inches"; $to = "centimeters"; } Continued on the top of the next column. Software error: Global symbol "$from" requires explicit package name at cmin2.cgi line 24. Global symbol "$ans" requires explicit package name at cmin2.cgi line 24. Global symbol "$to" requires explicit package name at cmin2.cgi line 24. Execution of cmin2.cgi aborted due to compilation errors.

7 payroll.txt: Susan Ash, 12 Monument Court, Hingham, MA, 02043, S, 1,, 50000 Jonathan English, 6 Madison Rd, Hingham, MA, 02043, S, 2,, 45000 Jennifer Ames, 62 Ravens Place, Braintree, MA, 02184, S, 3,, 60000 Stephen Daniels, 786 Meyer St, Hingham, MA, 02043, S, 1,, 60000 David Souza, 54 High St, Braintree, MA, 02184, F, 4, 45, Ann Rogers, 5123 Main St, Fall River, MA, 02720, P, 3, 25, Cynthia Adams, 60 Elm St, Fall River, MA, 02720, F, 1, 35, Eric Wong, 7 Bailey Circle, Fall River, MA, 02720, S, 3,, 45000 Charles Fredericks, 45 Main St, Hingham, MA, 02043, P, 3, 75, Sarah Grant, 498 East St, Hingham, MA, 02043, F, 3, 60, Carl Henry, 48 Tremont St, Braintree, MA, 02184, P, 2, 75, Jill Elliot, 23 Walnut St, Braintree, MA, 02184, F, 3, 22,

8 #!/usr/bin/perl #stringfunc.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($id, $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(/,/,$_); createid(); print "The id is: $id The name is: $name \n"; } close(INF); print " \n"; exit; #********user defined functions**************** sub createid { my($namefst3, $statelc, $citymid,$jc3); $namefst3 = lcfirst(substr($name,0,3)); $statelc = lc($state); $citymid = lc(substr($city,3,2)); $jc3 = $jobcode x 3; $id = $namefst3.$statelc.$jc3.$citymid; return $id; } This is the first record in payroll.txt that I am going to use for this example: Susan Ash, 12 Monument Court, Hingham, MA, 02043, S, 1,, 50000 This is the output: The id is: sus ma 1 1 1ng This code: substr($name,0,3) starts with character 0 and takes 3 characters in $name giving Sus. It is embedded in lcfirst which converts the first character to lower case which gives sus This converts $state to lower case giving ma This takes the substr starting with the third character and taking 2. It is nested with the lc function which converts to lower case giving ng This returns $jobcode 3 times which gives 1 1 1

9

10 payroll1.txt: Susan Ash,12 Monument Court,Hingham,MA,02043,S,1,,50000 Jonathan English,6 Madison Rd,Hingham,MA,02043,S,2,,45000 Jennifer Ames,62 Ravens Place,Braintree,MA,02184,S,3,,60000 Stephen Daniels,786 Meyer St,Hingham,MA,02043,S,1,,60000 David Souza,54 High St,Braintree,MA,02184,F,4,45, Ann Rogers,5123 Main St,Fall River,MA,02720,P,3,25, Cynthia Adams,60 Elm St,Fall River,MA,02720,F,1,35, Eric Wong,7 Bailey Circle,Fall River,MA,02720,S,3,,45000 Charles Fredericks,45 Main St,Hingham,MA,02043,P,3,75, Sarah Grant,498 East St,Hingham,MA,02043,F,3,60, Carl Henry,48 Tremont St,Braintree,MA,02184,P,2,75, Jill Elliot,23 Walnut St,Braintree,MA,02184,F,3,22,

11 #!/usr/bin/perl #stringfunc.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($id, $name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll1.txt") or die "file error: payroll1.txt. $!, stopped"; while( ) { chomp($_); ($name,$stadr,$city,$state,$zip,$jobtype,$jobcode,$payperhr,$salary) = split(/,/,$_); createid(); print "The id is: $id The name is: $name \n"; } close(INF); print " \n"; exit; #********user defined functions**************** sub createid { my($namefst3, $statelc, $citymid,$jc3); $namefst3 = lcfirst(substr($name,0,3)); $statelc = lc($state); $citymid = lc(substr($city,3,2)); $jc3 = $jobcode x 3; $id = $namefst3.$statelc.$jc3.$citymid; return $id; }

12

13 #!/usr/bin/perl #stringfuncmore.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($wherea, $name, $stadr, $city, $state, $zip, $jobtype, $jobcode, $payperhr, $salary); open(INF, "<", "payroll1.txt") or die "file error: payroll1.txt. $!, stopped"; while( ) { chomp($_); ($name,$stadr,$city,$state,$zip,$jobtype,$jobcode,$payperhr,$salary) = split(/,/,$_); finda(); print "The city is: $city The a is located at position: $wherea \n"; } close(INF); print " \n"; exit; #********user defined functions**************** sub finda { $wherea = index($city,"a"); return $wherea; } I am using index to check $city and tell me the location of the first a. Note that the answer will use a base 0.

14 The F of Fall River is in the 0th position and the a is in the 1st postion. The B of Braintree is in the 0th position, the r is in the 1st position and the a is in the second position.

15 #!/usr/bin/perl #stringfuncmore.cgi - reads the payroll file and processes with functions use CGI::Carp qw(fatalsToBrowser); print "Content-type: text/html\n\n"; use CGI qw(:standard); use strict; #declare variables my ($sec,$min,$hour,$mday,$mond,$year,$wday,$yday,$isdst,$myhour,$mymond, $myyear); print " Date \n"; print " \n"; print " Bristol Community College \n"; checkdate(); print " \n"; exit; #********user defined functions**************** Note that I did all the right things here - starting with html etc, you can get away with out doing this as I did on some of the previous examples. This program is going to perform the checkdate() user defined function which is on the next slide.

16 sub checkdate { ($sec,$min,$hour,$mday,$mond,$year,$wday,$yday,$isdst) = localtime(time); $myhour = $hour; $mymond = $mond +1; $myyear = $year + 1900; print " The date is: $mymond/$mday/$myyear \n"; print " "; if ($myhour >=8 and $myhour < 16) { print "Day school schedule\n"; } else { if ($myhour >=16 and $myhour < 22) { print "Evening schedule\n"; } else { print "The campus is closed\n"; } print "</font";

17


Download ppt "Functions Please use speaker notes for additional information!"

Similar presentations


Ads by Google