Perl: Lecture 2 notes

(as taken by Deb Deppeler on 2/10/2003)

Announcements

  1. Instructor: Sharon Whiteman (whiteman@cs.wisc.edu)
  2. Link to web page at:  http://www.cs.wisc.edu/~whiteman
  3. Next class will meet from 5:00-6:00pm in 1325CS
  4. Assignment 1 has been posted.
  5. may be others that Deb missed

Logical Operators

Perl has C-style logical ops, that work like you expect.

           &&   ||   !          &   |

Perl also has a second set of logical ops that have lower precedence than expected. This can lead to subtle errors for casual Perl programmers.

           and   or   not   xor

Use the above logical with caution!

Lists and Arrays

Lists are like arrays, except they are not stored.

List

        ($a, $b, $a)                 # not stored

Array

        @array_name = ($a, $b, $a)   # stored in @array_name
        @u2 = ('Bono', 'Edge', 'Adam', 'Larry');  # Sharon's fav!

Will see shortcuts for assigning to arrays later.

To access Array Elements

        $u2[0]   # returns 'Bono'

Shortcuts

A range of values in a list, like ( 1, 2, 3, 4, 5 ) can be specified as (1..5)

You can assign character strings to an array using the qw function:

        @u2 = qw( Bono Edge Adam Larry );  # assigns 'Bono', 'Edge', ...
        @u2 = qw /Bono Edge Adam Larry/;   # assigns 'Bono', 'Edge', ...

More Array Assignments

Add to the end of an array (works for lists too)

        @array = ( @array2, $scalar1 ); # adds the scalar to the end of the array

Can assign literal elements from a list to variables in a list.

        ($a, $b, $c) = (10, 'February', 2003);

Operations on Arrays

push

Adds an item to the end of an array:

        push @array, $a;        # legal
        push @array, ($a, $b);  # legal
        push @array, @array;    # legal
        push @array;            # NOT LEGAL (but should be in Perl!)
        

pop

Deletes the item from the end of the array

        $last = pop @array;     # last element is gone
        $last = pop (@array);   # same as above
        pop @array;             # remove last element without saving it
        

unshift

Adds an item to the beginning of an array:

        unshift @array, $a;        # legal
        unshift @array, ($a, $b);  # legal
        unshift @array, @array;    # legal
        unshift @array;            # NOT LEGAL (but should be in Perl!)
        

shift

Deletes the item from the beginning of the array

        $last = shift @array;     # first element is gone
        $last = shift (@array);   # same as above
        shift @array;             # remove first element without saving it
        

sort

Sorts the elements of the array in ASCII-betical order. This is true even if all elements are numbers.

        @array = sort @array;       # sort into same array
        @array = sort (1,8,2,4,16); #  1 16 2 4 8
        

reverse

Reverses the order of all elements in the array.

        @array = reverse @array;       # reverses elements into same array
        @array = reverse (1,8,2,4,16); #  16 4 2 8 1
        

for Loops (NOT in Perl!)

Perl programmers don't do indices.

foreach Loops (Way Better!)

        # Steps through each element of the array
        foreach $element ( @array )  
        {                            # required in Perl
        
           print $element;           # prints contents of the element
           
        }                            # required

        # Count-controlled for loop
        foreach $element ( 1..5 )  
        {                      
        
        
        }
        

Default Variable:
   $_

The default variable is an artifact of script programming. It is automatically assigned by some operations and can be automatically used by some functions. The more you become familiar with Perl, the more you will like this variable.

        # Each element is assigned to $_ in turn
        foreach ( @array )           # legal
        {                            
           print $_;                 # prints the element
           print;                    # prints the element
        }                            
        

The default variable is not available for all functions. For example:

        push @array, $_;     # Legal
        push @array;         # NOT LEGAL.  Does not implicitly use $_;

The $_ variable must be assigned or you will get a warning if -w is used.

        # Steps through each element of the array
        foreach $element ( @array )  
        {                            
           print ;           # causes warning if $_ has not been assigned
        }  

while Loops (Way Better!)

        # While there is standard input, read into $line variable
        while $line ( <STDIN> )  
        {                            # required in Perl
           chomp $line;
           print $line;              # prints the line read
           
        } 

        # Same as above
        while ( <STDIN> )  
        {                            # required in Perl
           chomp $line;
           print $line;              # prints the line read
           
        } 

Hashes %hash

A third data type. Hashes are associative arrays. They store a value for each key.

        %hash = ( 'key1' => 'value1',
                  'key2' => 'value2',     # trailing comma is okay,
                                          # even if no key-value pair follows.
                );   

To assign a value for a key:

        $hash{$key} = $value;   # assign $value for $key

To access a value for a key:

        $value = $hash{$key}    # access the $value for $key

Multi-keyed Hashes

There are even multi-keyed hashes, so you don't have to do hashes of hashes of hashes, etc.

To assign a value for a multi-key:

        $hash{$key1, $key2} = $value;   # assign $value for $key1,$key2 combo

Regular Expressions

Assume that all students have some experience with regular expressions. Will focus on the details of regular expressions in Perl, not the basic utility.

        a*       -->  0 or more a's
        a+       -->  1 or more a's
        a?       -->  0 or 1 a (optional)
        .        -->  match any character except \n
                      use \. to match a '.'
                      
        

How do I match U-2 and U2 with a regex?

        U-?2     # case sensitive match

                        _
        abc+     -->  abc
                      ___
        (abc)+   -->  abc
        
        a|b      -->  a  or  b
        

How do I parse a phone book?

With a Literal or with the use of special codes

        Whiteman       262-6600

        Whiteman\s+\d\d\d-\d\d\d\d
        Whiteman\s+\d{3}\-\d{4}
        

Special Codes

        \d           digits
        \w           words -> digits or letters
        \s           whitespace
        
        \D           anything that's not a digit
        \W           anything that's not a word
        \s           anything that's not whitespace
        
        ^W           W must be at beginning of string to match
        W$           W must be at end of the string to match
        ^Whiteman$   Whiteman on a line by itself
        
        $            matches top and top\n