introduction to perl part iii

Post on 06-Jan-2016

17 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Introduction to Perl Part III. By: Bridget Thomson McInnes 6 Feburary 2004. Hashes. Hashes are like array, they store collections of scalars ... but unlike arrays, indexing is by name Two components to each hash entry: Keyexample : name Value example : phone number - PowerPoint PPT Presentation

TRANSCRIPT

Introduction to PerlIntroduction to Perl

Part IIIPart III

By: Bridget Thomson McInnesBy: Bridget Thomson McInnes

6 Feburary 20046 Feburary 2004

HashesHashes Hashes are like array, they store Hashes are like array, they store

collections of scalarscollections of scalars ... but unlike arrays, indexing is by name... but unlike arrays, indexing is by name Two components to each hash entry:Two components to each hash entry:

– KeyKey example : nameexample : name– Value Value example : phone numberexample : phone number

Hashes denoted with %Hashes denoted with %– Example : %phoneDirectoryExample : %phoneDirectory

Elements are accessed using {} (like [] Elements are accessed using {} (like [] in arrays)in arrays)

Hashes continued ...Hashes continued ... Adding a new key-value pairAdding a new key-value pair

$phoneDirectory{“Shirly”} = 7267975$phoneDirectory{“Shirly”} = 7267975

– Note the Note the $$ to specify “scalar” context! to specify “scalar” context! Each key can have only one valueEach key can have only one value

$phoneDirectory{“Shirly”} = 7265797$phoneDirectory{“Shirly”} = 7265797

# overwrites previous assignment# overwrites previous assignment Multiple keys can have the same valueMultiple keys can have the same value Accessing the value of a keyAccessing the value of a key

$phoneNumber =$phoneDirectory{“Shirly”};$phoneNumber =$phoneDirectory{“Shirly”};

Hashes and ForeachHashes and Foreach Foreach works in hashes as well!Foreach works in hashes as well!

foreach $person (keys %phoneDirectory) {foreach $person (keys %phoneDirectory) {

print “$person: $phoneDirectory{$person}”;print “$person: $phoneDirectory{$person}”;

}}

Never depend on the order you put Never depend on the order you put key/values in the hash! Perl has its own key/values in the hash! Perl has its own magic to make hashes amazingly fast!!magic to make hashes amazingly fast!!

Hashes and SortingHashes and Sorting The sort function works with hashes as The sort function works with hashes as

well well Sorting on the keysSorting on the keys

foreach $person (foreach $person (sortsort keys %phoneDirectory) keys %phoneDirectory) {{

print “$person : $directory{$person}\n”;print “$person : $directory{$person}\n”;

}}– This will print the phoneDirectory hash table This will print the phoneDirectory hash table

in alphabetical order based on the name of in alphabetical order based on the name of the person, i.e. the person, i.e. the key.the key.

Hash and Sorting cont...Hash and Sorting cont...

Sorting by valueSorting by value

foreach $person (sort {$phoneDirectory{$a} <=> foreach $person (sort {$phoneDirectory{$a} <=> $phoneDirectory{$b}} keys %phoneDirectory) {$phoneDirectory{$b}} keys %phoneDirectory) {

print “$person : $phoneDirectory{$person}\print “$person : $phoneDirectory{$person}\n”;n”;

}}

– Prints the person and their phone number in Prints the person and their phone number in the order of their respective phone numbers, the order of their respective phone numbers, i.e. i.e. the value.the value.

A Quick Program using A Quick Program using HashesHashes

Count the number of Republicans in an Count the number of Republicans in an arrayarray

%seen = (); # initialize hash to empty%seen = (); # initialize hash to empty

@politArray = ( “R”, “R”, “D”, “I”, “D”, “R”, “G” @politArray = ( “R”, “R”, “D”, “I”, “D”, “R”, “G” ););

foreach $politician (@politArray) {foreach $politician (@politArray) {

$seen{$politician}++;$seen{$politician}++;

}}

print “Number of Republicans = $seen{'R'}\n”;print “Number of Republicans = $seen{'R'}\n”;

Slightly more advanced Slightly more advanced programprogram

Count the number of parties represented, Count the number of parties represented, and by how much!and by how much!

%seen = (); # initialize hash to empty%seen = (); # initialize hash to empty

@politArray = ( “R”, “R”, “D”, “I”, “D”, “R”, “G” );@politArray = ( “R”, “R”, “D”, “I”, “D”, “R”, “G” );

foreach $politician (@politArray) {foreach $politician (@politArray) {

$seen{$politician}++;$seen{$politician}++;

}}

foreach $party (keys %seen) {foreach $party (keys %seen) {

print “Party : $party. Num reps: print “Party : $party. Num reps: $seen{$party}\n”;$seen{$party}\n”;

}}

Command Line ArgumentsCommand 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 @ARGVQuick 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.

Another Example ProgramAnother Example Program You want to print the binary form of an integer You want to print the binary form of an integer

#!/usr/local/bin/perl -w#!/usr/local/bin/perl -w

foreach $integer (@ARGV) {foreach $integer (@ARGV) {

# converts the integer to a 32 bit binary number # converts the integer to a 32 bit binary number

@binary=split//,unpack(“B32”,pack(“N”,$integer)); @binary=split//,unpack(“B32”,pack(“N”,$integer));

# Store the last 4 elements of @binary into @bits# Store the last 4 elements of @binary into @bits

@bits = @binary[28..$#binary];@bits = @binary[28..$#binary];

# Print the integer and its binary form# Print the integer and its binary form

print “$integer : @bits\n”;print “$integer : @bits\n”;

}}

$_$_

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$_ 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$_ 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..$_ 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 ProgramExample Program Count the number of words in a text and Count the number of words in a text and

display the top 10 most frequency words.display the top 10 most frequency words.#!/usr/local/bin/perl#!/usr/local/bin/perl

%vocab = (); $counter = 0;%vocab = (); $counter = 0;

while(<>) {while(<>) {

chomp; chomp;

foreach $element (split/ /) { $vocab{$element}foreach $element (split/ /) { $vocab{$element}++; }++; }

}}

foreach $word (sort foreach $word (sort {$vocab{$b}<=>$vocab{$a}} %vocab) {{$vocab{$b}<=>$vocab{$a}} %vocab) {

print “$word $vocab{$word}\n”;print “$word $vocab{$word}\n”;

if($counter == 10) { last; } $counter++;if($counter == 10) { last; } $counter++;

}}

$_ and Regular Expressions$_ 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 ModulesPerl Modules

What are Perl Modules?What are Perl Modules?– Batches of reusable codeBatches of reusable code– Allow for object oriented Perl ProgrammingAllow for object oriented Perl Programming

Comprehensive Perl Archive Network (CPAN)Comprehensive Perl Archive Network (CPAN)– Perl utilitiesPerl utilities– Perl ModulesPerl Modules– Perl documentationPerl documentation– Perl distributionPerl distribution

CPAN OrganizationCPAN Organization CPAN Material is organized byCPAN Material is organized by

– ModulesModules– DistributionsDistributions– DocumentationDocumentation– AnnouncementsAnnouncements– PortsPorts– ScriptsScripts– AuthorsAuthors

Distributions are ‘tar-gzipped’Distributions are ‘tar-gzipped’– tartar– gzipgzip

Categories of ModulesCategories of Modules

by-authorby-author– Modules are organized by author’s registered Modules are organized by author’s registered

CPAN nameCPAN name by-categoryby-category

– Modules categorized by subject matterModules categorized by subject matter by-moduleby-module

– Modules categorized by module nameModules categorized by module name

Installing a ModuleInstalling a Module After you have gunziped and untared your After you have gunziped and untared your

module you have two options on installing your module you have two options on installing your module depending upon if you have root module depending upon if you have root privileges to the location where perl modules are privileges to the location where perl modules are installed or you don’t.installed or you don’t.

If you have root privileges or write access:If you have root privileges or write access: perl Makefile.PLperl Makefile.PL makemake make testmake test make installmake install

Local InstallLocal Install

Need to specify where you would like the module Need to specify where you would like the module to be installed by setting the PREFIX argument to be installed by setting the PREFIX argument when generating a Makefile from Makfile.PLwhen generating a Makefile from Makfile.PL

– perl Makefile.PL PREFIX=/home/Perl/Modulesperl Makefile.PL PREFIX=/home/Perl/Modules– makemake– make testmake test– make installmake install

Local Install cont…Local Install cont…

Perl usually looks in system wide areas for Perl usually looks in system wide areas for modules, therefore it will not find your local modules, therefore it will not find your local module unless you tell Perl where to find it.module unless you tell Perl where to find it.

In your perl program where you will be using your In your perl program where you will be using your modulemodule

#!/usr/local/bin/perl#!/usr/local/bin/perl

use lib ‘<module location>’use lib ‘<module location>’

use ModuleName;use ModuleName;

‘‘using’ a Perl Moduleusing’ a Perl Module If we have a module called TestModule that If we have a module called TestModule that

contains a function test_me(). To use this module contains a function test_me(). To use this module we have two options:we have two options:

– Object OrientedObject Orienteduse TestModule;use TestModule;$test = TestModule->new()$test = TestModule->new()$test->test_me()$test->test_me()

– StandardStandarduse TestModuleuse TestModuletest_me();test_me();

Example ProgramExample Program

#!/usr/local/bin/perl#!/usr/local/bin/perl

use lib ‘home/cs/bthomson/PerlModules/Suffix.pm’use lib ‘home/cs/bthomson/PerlModules/Suffix.pm’

use Suffix.pmuse Suffix.pm

$sarray->Array::Suffix->new();$sarray->Array::Suffix->new();

$sarray->create_files(“hamlet.txt”);$sarray->create_files(“hamlet.txt”);

$sarray->get_ngrams();$sarray->get_ngrams();

Module DocumentationModule Documentation

Perl Module Documentation is provided by the Perl Module Documentation is provided by the module author and usually written in pod formatmodule author and usually written in pod format

To view the documentation on the command lineTo view the documentation on the command line– %perldoc modulename%perldoc modulename

Convert the pod document to the format of your Convert the pod document to the format of your choice:choice:– pod2textpod2text converts to a text fileconverts to a text file– pod2htmlpod2html converts to an html fileconverts to an html file– pod2manpod2man converts to a man page fileconverts to a man page file

Thank you Thank you

top related