teaching machines

CS 330 Lecture 2 – Regular expressions

January 25, 2012 by . Filed under cs330, lectures, spring 2012.

Agenda

Scripting languages

John Ousterhout once said:

Scripting languages are designed for different tasks than are system programming languages, and this leads to fundamental differences in the languages. System programming languages were designed for building data structures and algorithms from scratch, starting from the most primitive computer elements such as words of memory. In contrast, scripting languages are designed for gluing: They assume the existence of a set of powerful components and are intended primarily for connecting components. System programming languages are strongly typed to help manage complexity, while scripting languages are typeless to simplify connections among components and provide rapid application development.

Read more in Scripting: Higher-level Programming for the 21st Century.

Scripting languages tend:

Enough Perl for today

Regular expressions

Code

test.pl

#!/usr/bin/perl

$var = 7;
print("$var is neat.\n");

print("Number of args: ", $#ARGV + 1, "\n");

for ($i = 0; $i <= $#ARGV; ++$i) {
  print("$ARGV[$i]\n");
}

base.pl

#!/usr/bin/perl

$line = $ARGV[0];

if ($line =~ m/^[01]+b$/) {
  print("You are binary!\n");
} elsif ($line =~ m/^([0-9A-Fa-f]+h|0x[0-9A-Fa-f]+)$/) {
  print("You are hexadecimal!\n");
} else {
  print("You are something else!\n");
}

states.txt

Alabama - Montgomery
Alaska - Juneau
Arizona - Phoenix
Arkansas - Little Rock
California - Sacramento
Colorado - Denver
Connecticut - Hartford
Delaware - Dover
Florida - Tallahassee
Georgia - Atlanta
Hawaii - Honolulu
Idaho - Boise
Illinois - Springfield
Indiana - Indianapolis
Iowa - Des Moines
Kansas - Topeka
Kentucky - Frankfort
Louisiana - Baton Rouge
Maine - Augusta
Maryland - Annapolis
Massachusetts - Boston
Michigan - Lansing
Minnesota - St. Paul
Mississippi - Jackson
Missouri - Jefferson City
Montana - Helena
Nebraska - Lincoln
Nevada - Carson City
New Hampshire - Concord
New Jersey - Trenton
New Mexico - Santa Fe
New York - Albany
North Carolina - Raleigh
North Dakota - Bismarck
Ohio - Columbus
Oklahoma - Oklahoma City
Oregon - Salem
Pennsylvania - Harrisburg
Rhode Island - Providence
South Carolina - Columbia
South Dakota - Pierre
Tennessee - Nashville
Texas - Austin
Utah - Salt Lake City
Vermont - Montpelier
Virginia - Richmond
Washington - Olympia
West Virginia - Charleston
Wisconsin - Madison
Wyoming - Cheyenne

stater.pl

#!/usr/bin/perl

open($in, '<', $ARGV[0]) or die("Couldn't open file. Boo hoo!");

print("State states[] = {\n");
while ($line = <$in>) {
  $line =~ m/(.*) - (.*)/;
  print("  new State(\"$1\", \"$2\"),\n");
}
print("};\n");

close($in);

TODO

Haiku

Doctor, mechanic–
Jobs of my dream family!
Dentist, regexpert…