Functions Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Advertisements

Reports Using SQL Script Please check speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Computer Science 1620 Loops.
1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CSC3530 Software Technology Tutorial Two PERL Basics.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
DBM Databases Please use speaker notes for additional information!
Perl/cgi and an Access Database Please use speaker notes for additional information!
SQL Use of Functions Character functions Please use speaker notes for additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
CGI and Perl - Basics Please use speaker notes for additional information!
Perl Web Page – Just Enough Pepper. Web site Set up the top of your script to indicate perl and plain text #!/usr/bin/perl print "Content-type:text/plain\n\n";
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Building PERL Scripts on a Windows system* *and running those scripts on an Apache server!
Data files using cgi/perl Please use speaker notes for additional information!
XML, DTD, ENTITY Please see speaker notes for additional information!
Logic Structure - focus on looping Please use speaker notes for additional information!
Mail with Perl/CGI Please use speaker notes for additional information!
XML and DTD Please you speaker notes for additional information!
1 Basic Perl CGI Programming. 2 Issues How and when your program is invoked. Generating Response –HTTP Headers –HTML (or whatever document type you want)
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
Bioinformatics 生物信息学理论和实践 唐继军
More on views Please refer to speaker notes for additional information!
CS0004: Introduction to Programming Project 1 – Lessons Learned.
SQL and Conditions Speaker notes will provide additional information!
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Exceptions in PL/SQL Please use speaker notes for additional information!
Introduction to CGI/Perl Please use speaker notes for additional information!
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Kailey Clemons November 6, 2014.
Sending data, forms and variables Please use speaker notes for additional information!
Introduction to Oracle - SQL Additional information is available in speaker notes!
Introduction to ASP.NET Please use speaker notes for additional information!
Label Assignment Please use speaker notes for additional information!
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
IF statements - selection Please use speaker notes for additional information!
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Analysis of SAMPLE1.CBL Please check speaker notes for additional information!
Introduction to PL/SQL As usual, use speaker notes for additional information!
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
PHP with MYSQL Please use speaker notes for additional information!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
February 7, You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,
More SQL functions As usual,there is additional information in the speaker notes!
Sending data with CGI/Perl Please use speaker notes for additional information!
University of Kansas Department of Electrical Engineering and Computer Science Dr. Susan Gauch April 21, 2005 I T T C Introduction to Web Technologies.
Chapter 7 - Introduction to Common Gateway Interface (CGI)
Basic concepts of C++ Presented by Prof. Satyajit De
Dimensional Analysis.
CGI I: Basics Web Programming.
Chapter 5 - Control Structures: Part 2
Introduction to Procedures
Introduction to the Array
MySQL - Creating donorof database offline
Please use speaker notes for additional information!
PROBLEM SOLVING CSC 111.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Presentation transcript:

Functions Please use speaker notes for additional information!

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 Enter the type of conversion: Centimeters to Inches Inches to Centimeters Enter the number to convert:

#!/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.

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"; }

#!/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.

#!/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.

payroll.txt: Susan Ash, 12 Monument Court, Hingham, MA, 02043, S, 1,, Jonathan English, 6 Madison Rd, Hingham, MA, 02043, S, 2,, Jennifer Ames, 62 Ravens Place, Braintree, MA, 02184, S, 3,, Stephen Daniels, 786 Meyer St, Hingham, MA, 02043, S, 1,, 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,, 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,

#!/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,, 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

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,

#!/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; }

#!/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.

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.

#!/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.

sub checkdate { ($sec,$min,$hour,$mday,$mond,$year,$wday,$yday,$isdst) = localtime(time); $myhour = $hour; $mymond = $mond +1; $myyear = $year ; 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";