Meeting 12 November 1819 2014 Short Exam Results Computer programming requires precise notation Missing Wrong case Mismatched parentheses There were many sloppy answers to questions The reprise on the next slides gives you the opportunity to be precise ID: 563071
Download Presentation The PPT/PDF document "Programming Languages" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Programming Languages
Meeting 12
November 18/19, 2014Slide2
Short Exam Results
Computer programming requires precise notation:
Missing ;
Wrong case
Mismatched parentheses
There were many sloppy answers to questions. The reprise on the next slides gives you the opportunity to be precise.Slide3
Short Exam Reprise
Submit by Friday, November 20, 2014, no later than 5:00 p.m. a text file attached to an email message containing the implementation in Lisp using our development rules of the functions:
reverse
concat
IGSlide4
Short Exam AnswersSlide5
GetFloat Project
Code checked
Comments
getc
implemented
getfloat
implemented
input file specification
Output checked
File of correct floats
File of floats with line numbers
File of floats mingled with character strings
File with no floats, but some look temptingly goodSlide6
Matrix Project
Helpfulness of
LispWorks
Bug when functions are redefined
Solution: Define
myfirst
,
mylast
Primitives
and
functionals
submitted?
Questions?Slide7
Preparations for Tonight
Unix cluster access?Slide8
Scripting Languages
Originally, tools for
Quick, dirty programs
Rapid prototyping for text-based computation
Glue between other programs
File format conversion
Evolved to mainstream programming tools
Check current estimates on the number of lines of
Javascript
code doing productive workSlide9
Characteristics
Strings as the basic, maybe only, data type
Associative arrays (hashes?) as the basic structured data type
Regular expressions as a principle programming structure
Minimal use of types and declarations
Usually interpreted rather than compiledSlide10
Examples
Unix shell
AWK
Perl
Python
Tcl
Javascript
VBScript, Jscript
PHPSlide11
Our Approach
Look at AWK and Perl first
Determine the syntactical structure of each language, starting with AWK
Use experimentation to discover the semantics
Work several exercises on each language during class timeSlide12
AWK
Age: 38 years
Developed at Bell Labs by
Al
Aho
, Brian Kernighan, Peter Weinberger
Aside: Where are each of these computing pioneers now?
Intended for simple manipulation of, and data extraction, from text files
38 years ago data existed in text files almost exclusivelySlide13
AWK Examples
Print all lines longer than 80 characters.
length > 80
Replace the second field in each line by its logarithm.
{ $2 = log($2); print }
Add up the numbers in the first field and report the sum and average.
{ sum += $1 }
END { print sum, sum/NR }Slide14
AWK Syntax
<program> ::= [<
begstate
>] <
stateseq
> [<
endstate
>]
<
stateseq
> ::= <statement> {<statement>}
<statement> ::= <pattern> |
{
<action>
}
|
<pattern>
{
<action>
}
<
begstate
> ::=
BEGIN {
<action>
}
<
endstate
> ::=
END {
<action>
}Slide15
Heart of AWK
<pattern>
A regular expression
A numeric expression
A combination of the previous two
<action>
Executable code, similar in structure to CSlide16
Inferred Control Structure
Previous languages: sequence
Execute S1, then S2, then S3, …
AWK: a triply nested for-loop surrounding an if-then. Specifically
for each file
for each input line
for each pattern
if pattern matches input line then
actionSlide17
Program Development
Use your favorite text editor to create an AWK program file.
Run the program
awk
–f
myprog
[file1 file2 … ]
OR
for really short programs (one line)
awk
‘program’ [file1 file2 …]Slide18
Some Interesting Data
From the course website, under Resources, download the four data files
Flight Aware data
Moby Dick extract
Web form data
Classic AWK data on countries (these data come from the 1985 AWK manual)Slide19
Some AWK Programs
Try each of these programs using
flightaware.txt
as the input file
{ print NR, $0 }
{ $1 = NR; print }
Note that <action> is surrounded by { } and may consist of several statements
separated
by ;Slide20
More Programs
{print $NF}
/Cancelled/
END {print NR}Slide21
AWK Built-Ins
NF – number of fields on a line
NR – number of lines (records) in a file, actually the number of the current line read in the file
$k – name of the
k
th
field, count starts at
1
$0 – name of the current line
FILENAME – name of current input fileSlide22
More Built-Ins
FS – field separator, typically reset in the BEGIN action if something other than space is needed
OFS – output field separator
RS – record separator
ORS – output record separator
OFMT – output format Slide23
Your Turn
Write AWK programs to:
Determine how many flights were cancelled
Find the latest arriving flight from those in the list
List the different aircraft used for this flight number.