Scripting Languages COMS W4115 Prof Stephen A Edwards Spring 2002 Columbia University Department of Computer Science Scripting Languages Awk Higher level languages Named for its three developers More compact for small programs Alfred Aho Often not suitable for large systems Peter Weinberger The more popular ones Brian Kernighan Good for data file manipulation I use it for computing grades and other simple tasks Awk Perl Python Tcl Bourne Shell Simple Awk Program Simple Awk Program General structure of an Awk program Input file Each line a record Space separated fields employee pay rate hours worked Beth 10 0 Kathy 14 0 pattern action pattern action 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 0 10 3rd field z 3 z 0 print 1 2 3 pattern z 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 Kathy 140 3 0 print 1 2 3 produces Kathy 140 Mark 200 Susie 148 5 True before and after the file Condition String pattern match Boolean operators Awk One Liners Statistics in Awk Associative Arrays Word Counter Print every line 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 gsub remove punctuation for i 1 i NF i count i END for w in count print count w w sort rn 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 Run on 1 5 10 3 7 11 gives 6 data points 6 16667 average 3 92003 standard deviation Run on the Tiger reference manual produces 103 the 58 of 51 is 49 and 49 a 35 expression 32 The 29 Perl Wordcount in Perl Understandable wordcount in Perl Larry Wall s Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister usr bin perl while chop s g words split foreach word words count word open SORTER sort nr foreach word keys count print SORTER count word word n 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 So Why Perl Python Perhaps too many Equivalent ways to print STDIN Perhaps the most popular scripting language Perl designed by a sane man while STDIN print print while STDIN while defined STDIN print for STDIN print print while defined STDIN Despite its flaws it s very powerful Very clean syntax and semantics Almost has a good type system Large collection of libraries but not as big as Perl s Very few things can t be done in Perl Regular expression support but not as integrated as Perl s Many Perl statements come in prefix and postfix form Ability to make virtually every Unix system call Binary data manipulation 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 while while if if unless Fast flexible interpreter Ported everywhere Very very extensive collection of libraries Database access CGI HTML for the web Math IPC Time Wordcount in Python Python Classes Python s Merits usr bin env python class Complex def init self realpart imagpart self r realpart self i imagpart Good support for programming in the large 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 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 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 Tcl Tcl Syntax Python can be the more verbose language but Perl can be cryptic John Ousterhout s Tool Command Language was originally intended to be grafted on to an application to make it controllable Shell like command syntax Regular expression support more integrated with language in Perl Perl better known Probably comparable execution speeds Since become a general purpose scripting language Its syntax is quite simple although rather atypical for a programming language command argument argument All data is strings incl numbers and lists Macro like variable substitution set foo 123 abc bar 1 foo 3 More tricks possible in Perl Python more disciplined Tk a Tcl package provide graphical user interface widgets Tcl Tk may be the easiest way to write a GUI Python has the much cleaner syntax and semantics I know which language s programs I d rather maintain Tk has been connected to Perl and Python as well set foo 1 set bar 2 puts eval foo bar Print 3 Wordcount in Tcl Nifty Tcl Features Tk usr bin env tclsh Associative arrays Hello World in Tk 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 count Stephen 1 button b text Hello World command exit pack b Procedures set f open sort rn w foreach word array names count puts f count word word proc sum3 a b c return expr a b c An Editable Graph 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 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 Lists Command substitution lappend foo 1 lappend foo 2 foreach i foo puts i print 1 then 2 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 Set up graph itself canvas c relief raised width 450 height 300 pack w …
View Full Document
Unlocking...