introduction to perl part iii by: cedric notredame adapted from (bt mcinnes)
TRANSCRIPT
Introduction to Perl
Part III
By: Cedric Notredame
Adapted from (BT McInnes)
Hashes
Hashes are like array, they store collections of scalars
... but unlike arrays, indexing is by name (just like ine real life!!!)
Two components to each hash entry: Key example : name Value example : phone number
Hashes denoted with % Example : %phoneDirectory
Elements are accessed using {} (like [] in arrays)
Hashes continued ...
Adding a new key-value pair $phoneDirectory{“Shirly”} = 7267975
Note the $ to specify “scalar” context! Each key can have only one value
$phoneDirectory{“Shirly”} = 7265797 # overwrites previous assignment
Multiple keys can have the same value
Accessing the value of a key $phoneNumber =$phoneDirectory{“Shirly”};
Hashes and Foreach Foreach works in hashes as well!
foreach $person (keys (%phoneDirectory) ) {
print “$person: $phoneDirectory{$person}”;}
Never depend on the order you put key/values in the hash! Perl has its own magic to make hashes amazingly fast!!
Hashes and Sorting The sort function works with hashes as well Sorting on the keys
foreach $person (sort keys %phoneDirectory) {
print “$person : $directory{$person}\n”;
} This will print the phoneDirectory hash table in
alphabetical order based on the name of the person, i.e. the key.
Hash and Sorting cont... Sorting by value
foreach $person (sort {$phoneDirectory{$a} <=> $phoneDirectory{$b}} keys %phoneDirectory)
{print “$person :
$phoneDirectory{$person}\n”;
}
Prints the person and their phone number in the order of their respective phone numbers, i.e. the value.
Exercise
Chose your own test or use wget “http://www.trinity.edu/~mkearl/family.html”
Identify the 100 most frequent words
Counting Wordswhile ($l=<F>)
{
@w=split (/\s+/, $l);
foreach $word (@w)
{
$word=~s/[sx]$//;#plurial elimination
$seen{$word}++;
}
}
foreach $word (sort {$seen{$a}<=>$seen{$b}} keys %seen)
{
print “Word $word N: $seen{$word}\n”;
}
Command Line Arguments
Command line arguments in Perl are extremely easy. @ARGV is the array that holds all arguments passed
in from the command line. Example:
% ./prog.pl arg1 arg2 arg3 @ARGV would contain ('arg1', arg2', 'arg3)
$#ARGV returns the number of command line arguments that have been passed. Remember $#array is the size of the array!
Quick Program with @ARGV
Simple program called log.pl that takes in a number and prints the log base 2 of that number;
#!/usr/local/bin/perl -w$log = log($ARGV[0]) / log(2);print “The log base 2 of $ARGV[0] is $log.\n”;
Run the program as follows: % log.pl 8
This will return the following: The log base 2 of 8 is 3.
$_
Perl default scalar value that is used when a variable is not explicitly specified.
Can be used in For Loops File Handling Regular Expressions
$_ and For Loops
Example using $_ in a for loop
@array = ( “Perl”, “C”, “Java” );for(@array) { print $_ . “is a language I know\n”;}
Output : Perl is a language I know. C is a language I know. Java is a language I know.
$_ and File Handlers
Example in using $_ when reading in a file;
while( <> ) { chomp $_; # remove the newline char @array = split/ /, $_; # split the line on white space
# and
stores data in an array}
Note: The line read in from the file is automatically store in
the default scalar variable $_
$_ and File Handling cont..
Another example similar to the previous example:
while(<>) { chomp; # removes trailing newline chars @array = split/ /; # splits the line on white
# space and stores the data
# in the array }
Notes: The functions chomp and split automatically perform their
respective operations on $_.
Example Program
Count the number of words in a text and display the top 10 most frequency words.#!/usr/local/bin/perl
%vocab = (); $counter = 0;
while(<>) {
chomp;
foreach $element (split/ /) { $vocab{$element}++; }
}
foreach $word (sort {$vocab{$b}<=>$vocab{$a}} %vocab) {
print “$word $vocab{$word}\n”;
if($counter == 10) { last; } $counter++;
}
$_ and Regular Expressions
Example in using $_ when using regular expressions
while( <> ) { chomp;
$_=~s/[.!,;]/ /; $_=~s/I am/Why are you/;
print “$_?\n”;}
Input line : I am feeling down today.
Output : Why are you feeling down today?
Perl Modules
What are Perl Modules? Batches of reusable code Allow for object oriented Perl Programming
Comprehensive Perl Archive Network (CPAN) Perl utilities Perl Modules Perl documentation Perl distribution
CPAN Organization
CPAN Material is organized by Modules Distributions Documentation Announcements Ports Scripts Authors
Distributions are ‘tar-gzipped’ tar gzip
Categories of Modules
by-author Modules are organized by author’s registered CPAN
name by-category
Modules categorized by subject matter by-module
Modules categorized by module name
Installing a Module
After you have gunziped and untared your module you have two options on installing your module depending upon if you have root privileges to the location where perl modules are installed or you don’t.
If you have root privileges or write access: perl Makefile.PL make make test make install
Local Install
Need to specify where you would like the module to be installed by setting the PREFIX argument when generating a Makefile from Makfile.PL
perl Makefile.PL PREFIX=/home/Perl/Modules make make test make install
Local Install cont…
Perl usually looks in system wide areas for modules, therefore it will not find your local module unless you tell Perl where to find it.
In your perl program where you will be using your module
#!/usr/local/bin/perl
use lib ‘<module location>’
use ModuleName;
‘using’ a Perl Module
If we have a module called TestModule that contains a function test_me(). To use this module we have two options:
Object Orienteduse TestModule;$test = TestModule->new()$test->test_me()
Standarduse TestModuletest_me();
Example Program
#!/usr/local/bin/perl
use lib ‘home/cs/bthomson/PerlModules/Suffix.pm’
use Suffix.pm
$sarray->Array::Suffix->new();$sarray->create_files(“hamlet.txt”);$sarray->get_ngrams();
Module Documentation
Perl Module Documentation is provided by the module author and usually written in pod format
To view the documentation on the command line %perldoc modulename
Convert the pod document to the format of your choice: pod2text converts to a text file pod2html converts to an html file pod2man converts to a man page file
Thank you