Upload
aria
View
61
Download
0
Embed Size (px)
DESCRIPTION
System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007. References: Perl man pages Albert Lingelbach, Jr. [email protected]. Review. Intro to shell Intro to perl Questions from exercises ?. Arrays: introduction. - PowerPoint PPT Presentation
Citation preview
1
System AdministrationIntroduction to Scripting, PerlSession 5 – Fri 23 Nov 2007
References: Perl man pages
Albert Lingelbach, [email protected]
2
Review
Intro to shell Intro to perl Questions from exercises ?
3
Arrays: introduction
Arrays store a series of values that can be accessed by "index"
Index is an integer starting at 0 or 1; Perl array indexing starts at 0 the upper limit depends on the language or
computer In Perl, array variables begin with @
Example: my @namearray; To access an element: $namearray[i]
Example: $namearray [3] = "testing";
4
Arrays: initialization
The contents of an array can be initialized at declaration:my @namearray = ("geoff", "foibe", "oscar");
This is equivalent to:my @namearray;$namearray[0] = "geoff";$namearray[1] = "foibe";$namearray[2] = "oscar";
5
Arrays: size
The index of the last element can be found using the expression$#array Notice that this is a scalar expression
What is an expression that will return how many elements are in an array ?
What statements will print out every element of an array ?
6
Arrays: practicum
Number of elements in an array:$#array + 1@array in scalar context
Print every element of an array:for (my $idx = 0; $idx < @array; $idx++) {
print "array[$idx] = $array[$idx]\n";
}
7
Arrays: multiple dimensions
Arrays can have more than one dimension:my @gameboard = ((" ", " ", " "), (" ", " ", " "), (" ", " ", " ") );
$gameboard [1, 1] = "X";$gameboard [0, 0] = "O";$gameboard [1, 2] = "X";$gameboard [1, 0] = "O";
8
Arrays: miscellaneous
An array can be sortedmy @sorted = sort @myarray;
An array can be reversedmy @reversed = reverse @myarray;
Seeman -M /usr/perl5/man perldatafor more information
9
Hashes: introduction
Hashes store a series of values that can be accessed by "key"
Key is a scalar (string, number) which is unique among the set of keys in the hash
Value is another scalar, associated with the key
Hashes begin with % Example: my %phonebook;
To access an element: $hash{key} Example: $phonebook {"mangula"}
10
Hashes: initialization
The contents of a hash can be initialized at declaration:my %phonebook = ( "albert" => "0787487449", "mangula" => "0756181255" );
This is equivalent to:my %phonebook;$phonebook {"albert"} = "0787487449";$phonebook {"mangula" = "0756181255";
11
Hashes: useful functions
The set of keys in a hash:my @keys = keys %myhash;
The set of values in a hash:my @values = values %myhash;
Expression to return the number of elements in a hash ?
Code to display the contents of a hash ?
12
Hashes: practicum
Number of elements in a hash:my @arr = keys %hash;my $length = @arr;my $length = $#arr + 1;
Display contents of a hash:my @keys = keys %hash;for (my $idx = 0; $idx < @arr; $idx++) {
print "hash {$keys [$idx]} = " ."$hash {$keys [$idx]}\n";
}
13
Hashes: miscellaneous
Seeman -M /usr/perl5/man perldatafor more information
14
Regular Expressions: introduction Regular expressions (sometimes shortened
to "regexp"s) are used for matching text to patterns
Recall shell "wildcards": * ? Excellent for searching, data validation
15
Regular Expressions: statement syntax
"Matching" operator: =~ Pattern markers: / / Example:
if ($s =~ /$pattern/) { print "$s matches $pattern\n";}else { print "$s does not match $pattern\n";
}
16
Regular Expressions: pattern syntax
any letter or number character represents itself "abc" matches /abc/
| or: one of many sub-patterns () group sub-patterns
"one" or "two" or "three" match /(one|two|three)/
"one" matches /(one)/, /(on)(e)/ [] group single character options
"a" or "b" or "c" match /[abc]/ "af" or "ag" match /[abc][fgh]/ "ab" does not match /[abc][fgh]/
17
Regular Expression pattern syntax continued
[^ ] not in set "a" or "b" matches /[^fgh]/ "f" does not match /[^fgh]/
Placement: ^ beginning of string $ end of string
"abc" matches /abc/, /^abc/, /abc$/, /^abc$/ "xabc" matches /abc/, /abc$/ "xabc" does not match /^abc/ or /^abc$/ "abcx" matches /abc/, /^abc/ "abcx" does not match /abc$/, /^abc$/
18
Regular Expression pattern syntax continued
Quantifiers ( )? zero or one
"caa", "abad" match /ab?a/ "abba" does not match /ab?a/ "acb", "yafoob3" match /a(foo)?c/ "afoofooc" does not match /a(foo)?c/
( )* zero or more "xaa", "yabbbbbbba" match /ab*a/
( )+ one or more "aa", "abbbb" do not match /ab+a/
19
Regular Expression pattern syntax continued
More quantifiers ( ){n} exactly n occurrences
"afoofoofoob" matches /a(foo){3}b/ "afoofoob" does not match /a(foo){3}b/
( ){i, j} between i and j occurrences "afoobarbarb" matches /a(foo){1,3}(bar){2,4}b/
"afoobarbarb" does not match /a(foo){2,3}(bar){2,4}b/
( ){i, } i or more occurrences "afoob" does not match /a(foo){2,}b/ "afoofoofoofoofoofoob" does match /a(foo){2,}b/
20
Regular Expression pattern syntax continued
\ is "escape" character use in front of special character to match it
literally "a(b" matches /a\(b/ /a]b/ is not a correct regular expression
use as marker for pre-defined sets/patterns (next slide)
. (dot, period) matches any one character "abb", "a(b" match /a.b/ "abx" does not match /a.b/
21
Regular Expression pattern syntax continued
Pre-defined patterns \s whitespace (space, tab, newline) \S non-whitespace character (same as [^\s]) \d digit \w word character (a-z, A-Z, 0-9, _)
22
Regular Expressions: miscellaneous
There are more pre-defined characters There are more features For more information:man -M /usr/perl5/man perlrequickman -M /usr/perl5/man perlretutman -M /usr/perl5/man perlre
23
Review
Arrays: @array = ("zero", "one", "two"); $array[idx]; $#array; sort @array; reverse @array;
Hashes: %hash = ("a" => 1, "b" => 2);$hash {$key};keys %hash; values %hash;
Regular expressions: =~ // . ? + * () [] [^] ^ $ {,}
24
Next up: Process management No class until January See e-mail for exercises e-mail any questions