teaching machines

CS 330 Lecture 2 – Regular expressions

Agenda

  • Activate your Piazza account!
  • Adventure, Zork
  • Imperative languages
  • Scripting languages
  • Perl

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:

  • to not be explicitly compiled,
  • to not require declarations,
  • to be terse,
  • and to play fast and loose with types.

Enough Perl for today

  • First line is path to interpreter: #!/usr/bin/perl.
  • Scalar variables are named $name.
  • Scalars can appear inside string literals: printf(“My name is $name.\n”). In Perl terms, such variables are interpolated.
  • my($var) creates a local variable, which goes out of scope at the end of the containing block.
  • local($var) temporarily reassigns (to undef) a global variable. However, its value is effectively restored at the end of the containing block.
  • Command-line arguments are in array @ARGV. The last index is $#ARGV. The first element is $ARGV[0].
  • Reading from a file is done with the diamond operator: <STDIN> or <$in>.
  • If assigning a read to a scalar, exactly one input record is consumed. By default, an input record is a line. To change the input record separator, reassign $/ to undef.

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

  • Homework 1

Haiku

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *