system administration introduction to scripting perl session 5 fri 23 nov 2007 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 PowerPoint Presentation
Download Presentation
System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007

Loading in 2 Seconds...

play fullscreen
1 / 24

System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007 - PowerPoint PPT Presentation


  • 141 Views
  • Uploaded on

System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007. References: Perl man pages Albert Lingelbach, Jr. alingelb@yahoo.com. Review. Intro to shell Intro to perl Questions from exercises ?. Arrays: introduction.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'System Administration Introduction to Scripting, Perl Session 5 – Fri 23 Nov 2007' - aria


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
system administration introduction to scripting perl session 5 fri 23 nov 2007
System AdministrationIntroduction to Scripting, PerlSession 5 – Fri 23 Nov 2007
  • References:
    • Perl man pages

Albert Lingelbach, Jr.

alingelb@yahoo.com

review
Review
  • Intro to shell
  • Intro to perl
  • Questions from exercises ?
arrays introduction
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";
arrays initialization
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";

arrays size
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 ?
arrays practicum
Arrays: practicum
  • Number of elements in an array:

$#array + 1

@arrayin scalar context

  • Print every element of an array:

for (my $idx = 0; $idx < @array; $idx++) {

print "array[$idx] = $array[$idx]\n";

}

arrays multiple dimensions
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";

arrays miscellaneous
Arrays: miscellaneous
  • An array can be sorted

my @sorted = sort @myarray;

  • An array can be reversed

my @reversed = reverse @myarray;

  • Seeman -M /usr/perl5/man perldatafor more information
hashes introduction
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"}
hashes initialization
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";

hashes useful functions
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 ?
hashes practicum
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";

}

hashes miscellaneous
Hashes: miscellaneous
  • Seeman -M /usr/perl5/man perldatafor more information
regular expressions introduction
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
regular expressions statement syntax
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";

}

regular expressions pattern syntax
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]/
regular expression pattern syntax continued
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$/
regular expression pattern syntax continued1
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/
regular expression pattern syntax continued2
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/
regular expression pattern syntax continued3
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/
regular expression pattern syntax continued4
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, _)‏
regular expressions miscellaneous
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
review1
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: =~ // . ? + * () [] [^] ^ $ {,}
next up
Next up:
  • Process management
  • No class until January
  • See e-mail for exercises
  • e-mail any questions