Presentation is loading. Please wait.

Presentation is loading. Please wait.

System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 References: Perl man pages Albert Lingelbach, Jr. alingelb@yahoo.com.

Similar presentations


Presentation on theme: "System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 References: Perl man pages Albert Lingelbach, Jr. alingelb@yahoo.com."— Presentation transcript:

1 System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007
References: Perl man pages Albert Lingelbach, Jr.

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 Example: To access an element: $namearray[i] Example: $namearray [3] = "testing";

4 Arrays: initialization
The contents of an array can be initialized at declaration: = ("geoff", "foibe", "oscar"); This is equivalent to: $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 in scalar context Print every element of an array: for (my $idx = 0; $idx $idx++) { print "array[$idx] = $array[$idx]\n"; }

7 Arrays: multiple dimensions
Arrays can have more than one dimension: = ( (" ", " ", " "), (" ", " ", " "), (" ", " ", " ") ); $gameboard [1, 1] = "X"; $gameboard [0, 0] = "O"; $gameboard [1, 2] = "X"; $gameboard [1, 0] = "O";

8 Arrays: miscellaneous
An array can be sorted = An array can be reversed = See man -M /usr/perl5/man perldata for 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" => " ", "mangula" => " " ); This is equivalent to: my %phonebook; $phonebook {"albert"} = " "; $phonebook {"mangula" = " ";

11 Hashes: useful functions
The set of keys in a hash: = keys %myhash; The set of values in a hash: = 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:
= keys %hash; my $length my $length = $#arr + 1; Display contents of a hash: = keys %hash; for (my $idx = 0; $idx $idx++) { print "hash {$keys [$idx]} = " . "$hash {$keys [$idx]}\n"; }

13 Hashes: miscellaneous
See man -M /usr/perl5/man perldata for 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 perlrequick man -M /usr/perl5/man perlretut man -M /usr/perl5/man perlre

23 Review = ("zero", "one", "two"); $array[idx]; $#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 for exercises any questions


Download ppt "System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 References: Perl man pages Albert Lingelbach, Jr. alingelb@yahoo.com."

Similar presentations


Ads by Google