CSCI A348/A548

Lecture Notes 10

Fall 1999


Review of Perl quiz questions. CGI processing with CGI.pm. A few more examples.

The

may be of use to you when you try to learn Perl.

The Learning Perl book is on reserve at Swain for A348.

In what follows we will be going over the quiz questions and justify the correct answers thus reviewing several aspects of Perl. When we are done we need to move into CGI.pm.

What's the output for this simple Perl program:
#!/usr/bin/perl
$a = "123_abc_456";
if ($a =~ /(\w+)/) { 
  print $1;
} else { print "No match"; }

Answer key: 123_abc_456

The matching operation in the if statement looks for a string of word characters (alphanumeric plus udnerscore) and the first non-empty string of such characters will be placed in $1, which will then be printed. $1 is a special Perl variable that stores intermediary results such as the pattern that matched the first set of parens. The whole initial string qualifies here. Notice that if the match is successful the result returned counts as true.


What's the output for this simple Perl program:
#!/usr/bin/perl
%myHash = ('Jordan' => 'awesome',
           'Bird'   => 'outstanding',
           'Kukoc'  => 'very good');
foreach $key (keys %myHash) { 
  print $key, ", ";
}

Answer key: you can't anticipate the order

The order in which the keys are listed for a hash table is not predictable, it reflects system (implementation) decisions that attempt to provide high-speed retrieval from such a structure. You can however use sort which sorts lexicographically by default.


What's the output for this simple Perl program:
#!/usr/bin/perl
%myHash = ('Jordan' => 'awesome',
           'Bird'   => 'outstanding',
           'Kukoc'  => 'very good');
foreach $key (sort (keys %myHash)) {
  print $key, ", ";
}

Answer key: Bird,Jordan,Kukoc,

It's the lexicographic order, by default.


What's the output for this simple Perl program:
#!/usr/bin/perl
@a = (4, 3, 2, 1);
print $a[$#a];

Answer key: 1

Last element in list.


What's the output for this simple Perl program:
#!/usr/bin/perl
$i = 0; $j = 1;
if ($i = 2) { print $i; }
else { print $j; }

Answer key: 2

Notice the assignment statement. It returns the value of its right-hand side.


What's the output for this simple Perl program:
#!/usr/bin/perl
$i = 0; $j = 1;
if ($i == 2) { print $i; } 
else { print $j; }

Answer key: 1

Compared to the previous problem this one correctly tests for equality.


What's the output for this simple Perl program:
#!/usr/bin/perl
$a = "bloomington";
$a =~ s/loo/lue/;
print $a;
Answer key: none of the above

Essentially bluemington, and that was not listed.


What's the output for this simple Perl program:
#!/usr/bin/perl
$a = "1:2:3:4";
@a = split(/:/, $a);
foreach $a (@a) { $var += $a; } 
print $var;
Answer key: 10

This sums all the elements in the list.


What's the output for this simple Perl program:
#!/usr/bin/perl
$a = 1;
print $a;
$a = $a + "something";
print $a;
Answer key: 11

The string amounts to 0 when evaluated as a number.


What's the output for this simple program:
#!/usr/bin/perl
$a = "There's a tomato in every automaton.";
$a =~ s/tomato/**/g;
print $a;
Answer key: There's a ** in every au**n.

There's a tomato in every automaton and we replace them all with stars.


What is the output of the following Perl program:
#!/usr/bin/perl
@a = (1, 2, 3, 4);
print $#a;
Answer key: 3

What's the index of the last element in the list?


Assume the following Perl program called one:
#!/usr/bin/perl
print $ARGV[1];
What is the output of the program when the program is called as follows:
./one my three arguments
Answer key: three

Second argument passed to the program is printed.


What's the output of this simple Perl program:
#!/usr/bin/perl
$a = "abc 123 my oh my patterns 456 oh boy patter3ns";
if ($a =~ /(\d+)/) {
  print $1;
} else { print "Nothing matches"; }
Answer key: 123

The first contiguous sequence of digits is identified (and through the use of parens it is stored in $1) and then printed.


What does the following program (called one)
#!/usr/bin/perl
print join(':', @ARGV);
produce when called as follows:
./one a b c d
Answer key: a:b:c:d

The arguments are put together in a string in a CGI fashion.


What's the output of this simple Perl program:
#!/usr/bin/perl
@a = ("one", "two", "three");
print join(':', @a);
Answer key: one:two:three

Same as previous problem except that it's a different list.


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "section=9014&language=perl&course=a348";
@a = split(/&/, $a);
foreach $a (@a) { 
  ($u, $v) = split(/=/, $a);
  $hash{$u} = $v;
}
print chop($hash{'section'});
Answer key: 4

Look at this CGI-like string. We almost ReadParse it, don't we? chop is a function that chops the last character of a string out and then returns it. The string is shortened by that character.


What's the output of this simple Perl program:
#!/usr/bin/perl
$a = ":4:3:2:1: ";
@a = split(/:/, $a);
print $#a;

Answer key: 5

The resulting list will have 6 elements, the first one is empty though.


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "this is my string";
print substr($a, 0, 7) . " good ";
Answer key: this is good

substr is going to extract from $a the characters that start at position 0 going through position 7 (8th character in the string that is). When we print this we add good to it.


What's the output of this simple Perl program:
#!/usr/bin/perl
$a = "4:3:2:1";
@a = split(/:/, $a);
print $#a;

Answer key: 3

We only need to pay attention at the splitting process and count the elements in the resulting list.


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "this is a string";
$a =~ s/(\W)/($1)/;
print $a;
Answer key: this( )is a string

This finds the first non-word character and surrounds it with parens. The parens in the left hand side of the pattern matching operator are supposed to help group characters and assign them to $1. The ones in the right hand side are simply literal characters. $1 is interpolated there.


What is the output of this simple Perl program:
#!/usr/bin/perl
$a = "abcde";
@a = split('', $a);
print $#a;
Answer key: 4

We split at nothing (or the empty string). How many nothings do you see in the string?


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "one"; $b = "two";
if ($a == $b) { print $a + $b; } 
elsif ($a < $b) { print $b; } 
else { print $a; } 
Answer key: 0

eq is for strings what == is for numbers. Remember that 1 + "something" evaluated to 1 before.


What's the output of this simple Perl program:
#!/usr/bin/perl
$a = "spippen:lbird:lbird:mjordan:lbird:mjordan";
@a = split(/:/, $a);
foreach $a (@a) { $valuefor{$a} += 2; }
print $valuefor{'mjordan'};
Answer key: 4

We count the number of occurrences (multipled by two) for each username.


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "This space for rEnt";
$a =~ s/[aeiou]//g;
print $a;
Answer key: Ths spc fr rEnt

Any character in that set goes away (globally). The set os the set of all vowels.


What's the output for this simple Perl program:
#!/usr/bin/perl
$a = "abc123def456";
$a =~ s/\d+/:-)/;
print $a;
Answer key: abc:-)def456

First contiguous string of digits is replaced by those three characters.


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "This is my song for the asking";
@a = split(/i/, $a);
print $a[0];
Answer key: Th

We split at i. What's $a[1] in that case?


What's the output for this simple Perl program:
#!/usr/bin/perl
$a = "abc123def456";
$a =~ s/\d*/:-)/;
print $a;
Answer key: :-)abc123def456

The first string of zero or more digits is replaced with a smiley. The question is where does the first such string occur?


What's the output of this small Perl program:
#!/usr/bin/perl
$a = "1::2:3:4";
$a =~ s/:+/:/g;
@a = split(/:/, $a);
print $a[1];
Answer key: 2

All contiguous strings of :'s are shortened to just one such character.


Last updated: September 30, 1999 by Adrian German