20070223

From Wsms

Jump to: navigation, search

previous next
GO TO:
Parent class notes: Perl Programming Language class notes
Back to: Perl programming language
George's complementary notes



Friday Feb 23 2007

Contents

About yesterday

What did you learn new about microsoft?
Are there any ideas for your final project? (Joe has 6 or more, but he didn't bring them)
Did you met someone? (2000 people and only one of us met someone!!!! What happens to us!!!)

More about regular expressions

The search and replace operation

It works like this:
s/searched_string/new_string/
That replaces the first ocurrence of the searched string and replaces it with the new string.
To replace all the ocurrences, you must add a "g" modifier for global: :
s/searched_string/new_string/g
That will replace all the ocurrences.

How to make a multiplier not greedy

You can make a multiplier only look for the first ocurrence of a multiple string, you follow the sign * by a question mark ?:
s/s{^/.*?}{}
will change /usr/share/dict/linux.word by /share/dict/linux.word.

From the book

Excercise 1

#!/usr/bin/env perl
# Write a program that looks through the perlfunc
# file for lines that start with
# =item and some whitespace, 
# followed by a Perl identifier name (made of letters,
# digits, and underscores, but never starting with a digit), 
# like the lines below. (There may be more text on the
# line after the identifier name; just ignore it.) 

# Have the program print each identifier name
# as it finds it; there will be hundreds of them,
# and many will appear more than once in the file. 

# As an example, the following lines of input
# resemble what you'll find in perlfunc

#    =item wilma 
#   =item fred flintstone

open IN, "perlfunc.txt" or die "Couldn't open file perlfunc.txt";

while (<IN>) {
    if (/^=item\s*([\w_][\w\d_]*)/) {
        print "$1\n";
    }
}

Excercise 2

#!/usr/bin/env perl

# Write a program that looks through the perlfunc
# file for lines that start with =item and some whitespace, 
# followed by a Perl identifier name (made of letters, digits,
# and underscores, but never starting with a digit), 
# like the lines below. (There may be more text on the line
# after the identifier name; just ignore it.) 

# Have the program print each identifier name as it finds it;
# there will be hundreds of them,
# and many will appear more than once in the file. 

# As an example, the following lines of input resemble
# what you'll find in perlfunc

#    =item wilma 
#   =item fred flintstone

# Modify the previous program to list only the identifier
# names that appear more than twice on those =item lines,
# and tell how many times each one appeared.
# (That is, we want to know which identifier names appear
# on at least three separate =item lines in the file.)
# There should be a couple of dozen, depending upon your version of Perl.

open IN, "perlfunc.txt" or die "Couldn't open file perlfunc.txt";

my %count = {};

while (<IN>) {
    if (/^=item\s*([\w_][\w\d_]*)/) {
        $count{$1} ++;
    }
};

foreach (sort keys %count) {
    ($count{$_} > 2) &&  print "$_ appears $count{$_} times\n";
}
Personal tools