2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; }...

25
2000 Copyrights, Danielle S. Lahmani Foreach example • Example: @numbers = ( 3, 5, 7, 9) foreach $one (@numbers ) { $one*=3; } # @numbers is now (9,15,21,27)

description

2000 Copyrights, Danielle S. Lahmani HASH: Examples Example: %carcolor = ("buick", "yellow"); $mycolor = $carcolor{ "buick"}; $mycolor is now "yellow" %copy = %original; # copy from %original to %copy

Transcript of 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; }...

Page 1: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Foreach example

• Example:@numbers = ( 3, 5, 7, 9)foreach $one (@numbers ) {

$one*=3;}

# @numbers is now (9,15,21,27)

Page 2: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

PERL: HASHES• A hash is a collection of scalar data with

individual elements selected by some index. Index values are arbitrary scalars called keys;

They are used to retrieve values from the array. Elements of a hash have no special order A hash is denoted by "%" sign Elements of a hash are referenced by

$hashname{key}

Page 3: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

HASH: Examples

• Example: %carcolor = ("buick", "yellow");

• $mycolor = $carcolor{ "buick"};• $mycolor is now "yellow"• %copy = %original; # copy from

%original to %copy

Page 4: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

HASH FUNCTIONS

• keys functions:• keys(%hashname) yields a list of the current

keys in the hash %hashname.• Example: keys(%hashname) = keys %hashname;

# once for each key of % fred foreach $key (keys (%fred)) { print "at $key we have $fred{$key} \n"; # show key and value

Page 5: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

HASH FUNCTIONS (CONT')• values function:• values(%hashname) yields a list of the

current valuesof %hashname in the same order as keys returned by keys(%hashname)

• %lastname = ("barney", "flinstone", "gerry", "smith");

• @lastname = values(%lastname); #grab the values

Page 6: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

HASH FUNCTIONS (each) each function:• each(%hashname) returns a key-value pair as

a two element list.• Used to iterate over an entire hash (examining

every element of ).Example:while (($first, $last)) = each(%lastname)) {

print "the last name of $first is $last\n";}

Page 7: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Hash function: delete

• removes hash elements, takes a hash reference as argument

• delete $lastname{"barney"}; #lastname has only one key-value pair now.

Page 8: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

CONTROL STRUCTURES Perl supports "if", "for" while" similar than those

in C. "foreach" constructs is from the C shell• foreach example:

– If the list we are iterating over is made of real variables Rather than some functions returning a list value,

– Then the variable being used for iteration is in fact An alias for each variable in the list instead of being A merely copy of the values

Page 9: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

BASIC I/O Input from STDIN• Perl uses the variable $_ to contain the line read

from STDIN.• $a = <STDIN>; #reads the next line• @a = <STDIN>; # reads all lines until control ^D• typically

while (defined ($line = <STDIN>) {# process $line here

}when no more lines read. <STDIN> returns undef.

Page 10: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

using the diamond operator <>

<> operates like <STDIN>, but gets data from file or files

• specified on the command line that invoked the PERL program.

• <> looks at the @ARGV array#!/usr/bin/perlwhile (<>) {

print $_;}

Page 11: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Output to STDOUT

• print for normal output• printf for formatted output

Page 12: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

REGULAR EXPRESSIONS PERL supports the same regular expressions

as in SED. =~ match operator• It takes a regular expression operator on the right side

and changes the target of the operator to some value.• The target of the =~ operator can be any expression

that yields some scalar string value.Example: if (<STDIN> = ~ /^[yY]/) {

print" what can I do for you? ";

Page 13: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Regular expressions: split function

• split function takes a regular expression and a string, and looks for all occurrences of the regular expression withinthat string.

• Parts of the string that don't match the regular expression are returned in sequence as a list of values

Page 14: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Example of split function

$line = "merlyn::118:120:Randal:/home/merlyn:/usr/bin/perl";

@fields = split(/:/, $line); # split $line, using : as delimiter

# now @fields is ("merlyn,"", "118", "120", "Randal",

# "/home/merlyn", "/usr/bin/perl")

Page 15: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

join function

• takes a list of values and glues them together with a glue string between each list element.

• Example: $outline = join(":", @fields);

Page 16: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

PERL :FUNCTIONS• Defining a user function

Sub subname {Statement_1;Statement_2;Statement_3;}

return value is the value of the return statement or of the last expression evaluated in the subroutine.

Page 17: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Function Arguments

• Subroutine invocation is followed by a list within parenthesis

• Causing the list to be automatically assigned to a special variable named @_ for the duration of the subroutine.

• $_[0] is the 1st element of the @_ array• @_ variable is private to the subroutine.

Page 18: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Function private variables

• using the my operatorsub add {

my ($sum); #make $sum a local variableforeach $_ ( @_) {$sum += $_; # add each element

}return $sum #last expression evaluated

}

Page 19: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Functions: Semi-private variables

semi-private variables using local• local variables are visible to functions

called from within the block in which those variables are declared.

Page 20: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

FILEHANDLES• Recommendation use all uppercase letters in your

filehandles .• 3 files handles, STDIN, STDOUT, STDERR for

standard in, out and error.open(FILEHANDLE, "filename");

>"filename">> "filename”

die: equivalent to "open that file or die."open(FILEHANDLE,>"filename") | |

die "Sorry could not create filename\n";• Perl provides -op file tests just like the shells.

Page 21: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Perl modules

• TBD

Page 22: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

USER DATABASE MANIPULATION

• Most UNIX systems have a standard library called DBM,

• Which allows programs to store a collection of key-value pairs

• Into a pair of disk files.• In Perl, a hash may be associated with a DBM through

a process• Similar to opening a file. • dbmopen function associates a DBM database with a

DBM array:

Page 23: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

Database interface example

dbmopen(%ARRAYNAME, "dbmfilename", $mode);

dbmopen(%FRED, ."mydatabase", 0644);delete $FRED{"barney"}while (($key, $value) = each(%FRED)) {print "$key has value of $value\n";

}

Page 24: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

perl debugging

• perl -d• h print out a help message• T stack trace• s insgle step• n next• f finish• c continue• q quit• D delete all breakpoints

Page 25: 2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is…

2000 Copyrights, Danielle S. Lahmani

SYSTEM CALLS

• Perl provides an interface to many UNIX system calls.

• Interface is via Perl functions, not directly through the system call library.

• The interface use is dependent on the implementation and version of Perl being used.