Unformatted text preview:

Scripting Languages COMS W4115 Prof Stephen A Edwards Fall 2006 Columbia University Department of Computer Science Scripting Languages Higher level languages More compact for small programs Often not suitable for large systems The more popular ones Awk Perl Python Tcl Bourne Shell Awk Named for its three developers Alfred Aho Peter Weinberger Brian Kernighan Good for data file manipulation I use it for computing grades and other simple tasks Simple Awk Program Input file Each line a record Space separated fields employee pay rate hours worked Beth 10 0 0 Dan 9 75 0 Kathy 14 0 10 Mark 10 0 20 Susie 8 25 18 Run on the awk program 3 0 print 1 2 3 produces Kathy 140 Mark 200 Susie 148 5 Simple Awk Program Beth 10 0 Kathy 14 0 0 10 3rd field z 3 0 print 1 2 3 z z pattern Kathy 140 action Awk Program Structure pattern action pattern action awk scans an input file one line at a time and in order runs each action whose pattern matches Patterns BEGIN END expression regular expression pattern pattern pattern pattern pattern True before and after the file Condition String pattern match Boolean operators Awk One Liners Print every line print Print the first and third fields of each line print 1 3 Print every line with three fields NF 3 print Print a line number before every line print NR 0 Statistics in Awk bin awk f BEGIN n 0 s 0 ss 0 NF 1 n s 1 ss 1 1 END print n data points m s 0 0 n print m average sd sqrt ss n m m n 1 0 print sd standard deviation Run on 1 5 10 3 7 11 gives 6 data points 6 16667 average 3 92003 standard deviation Associative Arrays Word Counter gsub remove punctuation for i 1 i NF i count i END for w in count print count w w sort rn Run on the Tiger reference manual produces 103 the 58 of 51 is 49 and 49 a 35 expression 32 The 29 Perl Larry Wall s Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister Larger more flexible language than Awk Good for text processing and other tasks Strange semantics Henious syntax Excellent regular expression support More complicated data structures possible even classes Wordcount in Perl usr bin perl while chop s g words split foreach words count open SORTER sort nr foreach keys count print SORTER count n Understandable wordcount in Perl usr bin perl while line chop line line s g words split s line foreach word words count word open SORTER sort nr foreach word keys count print SORTER count word word n There s more than one way to do it Perhaps too many Equivalent ways to print STDIN while STDIN print print while STDIN print while while defined STDIN print for STDIN print print while defined STDIN Many Perl statements come in prefix and postfix form while while if if unless So Why Perl Perhaps the most popular scripting language Despite its flaws it s very powerful Almost has a good type system Very few things can t be done in Perl Fast flexible interpreter Ability to make virtually every Unix system call Binary data manipulation Ported everywhere Very very extensive collection of libraries Database access CGI HTML for the web Math IPC Time Python Perl designed by a sane man Very clean syntax and semantics Large collection of libraries but not as big as Perl s Regular expression support but not as integrated as Perl s Wordcount in Python usr bin env python import fileinput re string os count for line in fileinput input line re sub r line for word in string split line if not count has key word count word 1 else count word count word 1 f os popen sort nr w for word in count keys f write d s n count word word Python Classes class Complex def init self realpart imagpart self r realpart self i imagpart def add self a self r self r a r self i self i a i def p self print g gi self r self i x Complex 1 2 y Complex 2 3 x p x add y x p Prints 1 2i 3 5i Python s Merits Good support for programming in the large Packages with separate namespaces Exceptions Classes Persistent datastructures pickling High level lists strings associative arrays iterators Good collection of libraries Operating system access files directories etc String manipulation Curses Databases Networking CGI HTTP URL mail Mime HTML Tk Cryptography System specific Windows Mac SGI POSIX Python vs Perl Python can be the more verbose language but Perl can be cryptic Regular expression support more integrated with language in Perl Perl better known Probably comparable execution speeds More tricks possible in Perl Python more disciplined Python has the much cleaner syntax and semantics I know which language s programs I d rather maintain Tcl John Ousterhout s Tool Command Language was originally intended to be grafted on to an application to make it controllable Since become a general purpose scripting language Its syntax is quite simple although rather atypical for a programming language Tk a Tcl package provide graphical user interface widgets Tcl Tk may be the easiest way to write a GUI Tk has been connected to Perl and Python as well Tcl Syntax Shell like command syntax command argument argument All data is strings incl numbers and lists Macro like variable substitution set foo 123 abc bar 1 foo 3 Command substitution set foo 1 set bar 2 puts eval foo bar Print 3 Wordcount in Tcl usr bin env tclsh while gets stdin line 0 regsub all line line foreach word line if info exists count word set count word 1 else incr count word set f open sort rn w foreach word array names count puts f count word word Nifty Tcl Features Associative arrays set count Stephen 1 Lists lappend foo 1 lappend foo 2 foreach i foo puts i print 1 then 2 Procedures proc sum3 a b c return expr a b c Tk Hello World in Tk button b text Hello World command exit pack b An Editable Graph An Editable Graph Set up the main window set w plot catch destroy w toplevel w wm title w Plot Demonstration wm iconname w Plot positionWindow w set c w c Text description at top label w msg font font wraplength 4i justify left text This window displays a canvas widget containing a simple 2 dimensional plot You can doctor the data by dragging any of the points with mouse button 1 pack w msg side top An Editable Graph Set up bottom control buttons frame w buttons pack w buttons side bottom fill x pady 2m button w buttons dismiss text Dismiss command destroy w button w buttons code text See Code command showCode w pack w buttons dismiss w buttons code side left expand 1 Set up graph itself canvas c relief raised width 450 height 300 pack w c side top fill x Draw …


View Full Document

Columbia COMS W4115 - Scripting Languages

Documents in this Course
YOLT

YOLT

13 pages

Lattakia

Lattakia

15 pages

EasyQL

EasyQL

14 pages

Photogram

Photogram

163 pages

Espresso

Espresso

27 pages

NumLang

NumLang

6 pages

EMPATH

EMPATH

14 pages

La Mesa

La Mesa

9 pages

JTemplate

JTemplate

238 pages

MATVEC

MATVEC

4 pages

TONEDEF

TONEDEF

14 pages

SASSi

SASSi

16 pages

JTemplate

JTemplate

39 pages

BATS

BATS

10 pages

Synapse

Synapse

11 pages

c.def

c.def

116 pages

TweaXML

TweaXML

108 pages

Load more
Loading Unlocking...
Login

Join to view Scripting Languages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scripting Languages and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?