COS 381 Day 21. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl.
Introduction to C Programming
Introduction to C Programming
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Lecture 2 Introduction to C Programming
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
COS 381 Day 18. Agenda  Assignment 4 Corrected 2 A’s, 3 C’s, 1 D and 1 F nment4/answer.htm
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
COS 381 Day 19. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
COS 381 Day 17. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
COS 381 Day 22. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Introduction to scripting
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Review of C++ Programming Part II Sheng-Fang Huang.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Chapter 7 Functions.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Learners Support Publications Classes and Objects.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Basic Program Construction
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
PERL. Introduction Perl is a flexible, powerful, widely used programming language. Perl is widely used for Common Gateway Interface (CGI) programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 9: Value-Returning Functions
Chapter 6 JavaScript: Introduction to Scripting
Java Primer 1: Types, Classes and Operators
JavaScript: Functions.
User-Defined Functions
Chapter 14 Introduction to Ruby.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
INPUT & OUTPUT scanf & printf.
Presentation transcript:

COS 381 Day 21

Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard In Class Work Assignment 5 is posted Due April 18 at 11:05 AM 3 rd and final Capstone progress report due April 25 Final Capstone presentation May 5 or May 10AM Continue Discussion on Perl Perl Documentation Perl Tutorial

ETS testing ETS Field Test in Business for Wednesday, April 23, in Nadeau 109. the morning; 9:00 a.m. afternoon 1:00 PM Juniors and Seniors in the accredited Professional Management Programs (ecommerce) should take this test. If a student took the test last year, they can take it again, if they want to.

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8 The Basics of Perl

8-5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.7 References A reference is a scalar value giving the address of another value in memory A reference to an existing variable is created by using the backslash operator References to literal structures can be created A reference to a list is created by enclosing a list in square brackets, […] A reference to a hash is created by enclosing a list in curly braces {…} For example $a = [1, 2, 3, 4] For example $h = {‘i’ => 1, ‘v’ => 5, ‘x’ => 10}; Notice the assignment is to a scalar variable since the literal value is a reference

8-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.7 Dereferencing References To access the value pointed to by a reference, the programmer must explicitly dereference the reference An extra $ sign can be used If $a = 5 and $b = \$a then $$b is 5 $$b = 7 changes the value of $a to 7 If it is a reference to an array or is the same as original array %$ref_hash is the same as the original hash In a reference to an array (or hash), -> can be used between the reference and the index to indicate a dereference If $r = then $$r[3] is the element at index 3 $r->[3] is also the element at index 3 $r[3] is the element at index 3 completely unrelated perl\refernces.pl

8-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Function Fundamentals A function definition consists of a function header and the body The body is a block of code that executes when the function is called The header contains the keyword sub and the name of the function A function declaration consists of the keyword sub and the function name A declaration promises a full definition somewhere else A function call can be part of an expression. In this case the function must return a value that is used in the expression A function call can be a standalone statement. In this case a return value is not required. If there is one, it is discarded

8-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Function Return When a function is called, the body begins executing at the first statement A return statement in a function body causes the function body to immediately cease executing If the return statement also has an expression, the value is returned as the value of the function Otherwise, the function returns no value If execution of a function reaches the end of the body without encountering a return statement, the return value is the value of the last expression evaluated in the function

8-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Local Variables Variables that are not declared explicitly but simply assigned have global scope The my declaration is used to declare a variable in a function body to be local to the function If a local variable has the same name as a global variable, the global variable is not visible within the function body Perl also supports a form of dynamic scoping using the local declaration A my declaration has lexical scope which works like scope rules in C, C++ and Java

8-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Parameters Parameters used in a function call are called actual parameters Formal parameters are the names used in the function body to refer to the actual parameters In Perl, formal parameters are not named in the function header Perl supports both pass-by-value and pass-by-reference The is initialized in a function body to the list of actual parameters An element of this array is a reference to the corresponding parameter: changing an element of the array changes the corresponding actual parameter Often, values are assigned to local variables which corresponds to pass-by-value If the values are manipulated in the function then it is pass- by-reference

8-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Parameter Usage Examples This code causes the variable $a to change sub plus10 { $ _ [0] += 10; } plus10($a); The first line of this function copies actual parameters to local variables Sub f { my($x, $y) $x = 2*$x; $y = 2*$y; print “In function $x \t $y \t $_[0] \t $_[1] \n”; return ($x, $y) } ($x, $y)= (2, 4); ($a, $b) = f($x,$y); print “$a \t $b \t $x \t $y”; perl\function_tricks.pl

8-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Passing Structures as Parameters An array or hash will be flattened if included directly in an actual parameter =(2, 3, 4, 5, 6); $_[3] = ? A reference to a hash or array will be passed properly since the reference is a scalar value $_[0] = ? =

8-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 sort Revisited The sort function can be called with the first parameter being a block which returns a numerical value based on the comparison of two variables $a and $b This parameter is not followed by a comma For example, using sort {$a will sort the using numerical comparison Using sort {$b will sort in reverse order

8-14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.8 Example The example tst_median illustrates a function that finds the median of an array passed as a reference valuetst_median

8-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.9 Basics of Pattern Matching Perl has powerful pattern matching facilities built in These have been imitated in a number of other systems Regular expressions were described in the JavaScript chapter The m operator indicates a pattern matching This is used with delimiters like q and qq but the enclosed characters form a pattern If the delimiter is / then the m is not required A match is indicated by the =~ operator with a string on the left and a pattern on the right A pattern alone is matched by default to $_ The split function can take a pattern as the first argument rather than a character The pattern specifies the pattern of characters used to split the string apart

8-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.9 An Example Example word_table.pl uses a pattern to split a text into wordsword_table.pl A hash table is used to count the frequency of each word The keys are the words, the corresponding values are the counts of the words in the text The exists function is used to tell if a word is already entered into the hash

8-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.9 Remembering Matches Parts of a pattern can be parenthesized If the pattern matches a string, the variables $1, $2, … refer to the parts of the string matched by the parenthesized sub-patterns “4 July 1776” =~ /(\d+) (\w+) (d+)/; print “$2 $1, $3”  July 4, 1776 If a match is successful on a string, three strings are available to give the context of the match $& is the part that actually matched the pattern $` is the part of the string before the part that matched $’ is the part of the string after the part that matched "Tony Gauvin" =~ /au/; print "$& $' $`. \n;« perl\reg_expession_implict.pl

8-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.9 Substitutions The s operator specifies a substitution s/pattern/new-string/ The new-string will replace the part of a string matched by the pattern The =~ operator is used to apply the substitution to a string If the operator is not used, $_ is operated on by default A g modifier on the substitution causes all substrings matching the pattern to be replaced, otherwise only the first match is changed The i modifier cause the pattern match to be case insensitive

8-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.9 The Transliterate Operator This is written tr/char-list1/char-list2/ When applied to a string it causes each character of the string that appears in the first list to be replaced by the corresponding character in the second list If the second list is empty, the characters from the first list are deleted from the string The =~ operator is used to apply the transliteration If the operator is not used, $_ is operated on by default $test-str = “I Love Perl Programming”; tr/P/p/; same as s/P/p/g; tr/aeiou/i;

8-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.10 File Input and Output To carry out file input and output, a filehandle must be created for each file The open function is used to create a file handle The first parameter to open is the name of a file handle By convention the name is all capital letters The second parameter to open is a string value naming the file and, optionally, including a character to indicate the mode of opening the file < indicates open for input (default) > indicates open for output, deleting the content of an existing file >> indicates open for output, appending to a file that already exists Read a file open(DAFILE, “<read.txt”) or die “whoops -$1”; Write a file open(DAFILE, “>write.txt”); Append to a file open(DAFILE, “>>append.txt”); Read and write to a file open(DAFILE, “+>readandwrite.txt”);

8-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.10 Input and Output Operations The print function is used to send output to a filehandle print OUTHANDLE “data”, “more data”; Note that there is not comma after the OUTHANDLE This is important, otherwise the value of the handle will be displayed on the output console The input operator <> can be used on an input file handle $next_line = ; The read function reads a number of characters into a given array read(FILE, $store, 255, 0); The function returns actual number of characters read The function parameters can indicate that characters are to be stored in the array somewhere other than at the beginning The seek function can be used to position the filehandle cursor at a different position in the file seek(FILE, offset, base~[0,1,2]) offset is the numbers of line to move (postive moves toward the EOF) 0 = beginning of file 1 =current cursor position 2 is EOF Examples seek(FILE, 0,0); seek(FILE, -1, 1);

8-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8.11 Example The example wages.pl illustrates many of the features of Perlwages.pl An input file contains lines of data with fields separated by colons The split function can be used to separate the fields Pattern matches are used on names A hash is used to store employees and their salaries More in class work Help in assignemnts

8-23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 card shuffle Put 3 cards in order If card b < card a swap card a and card b If card c < card b swap card c and card b If card b < card a swap card a and card b