System Administration Introduction to Scripting, Perl Session 5 - PowerPoint PPT Presentation

About This Presentation
Title:

System Administration Introduction to Scripting, Perl Session 5

Description:

'aa', 'abbbb' do not match /ab a/ 19. Regular Expression pattern syntax continued ... No class until January. See e-mail for exercises. e-mail any questions ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 25
Provided by: albertli
Category:

less

Transcript and Presenter's Notes

Title: System Administration Introduction to Scripting, Perl Session 5


1
System AdministrationIntroduction to Scripting,
PerlSession 5 Fri 23 Nov 2007
  • References
  • Perl man pages
  • Albert Lingelbach, Jr.
  • alingelb_at_yahoo.com

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 _at_
  • Example my _at_namearray
  • To access an element namearrayi
  • Example namearray 3 "testing"

4
Arrays initialization
  • The contents of an array can be initialized at
    declaration
  • my _at_namearray ("geoff", "foibe", "oscar")
  • This is equivalent to
  • my _at_namearray
  • namearray0 "geoff"
  • namearray1 "foibe"
  • namearray2 "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
  • _at_array in scalar context
  • Print every element of an array
  • for (my idx 0 idx lt _at_array idx)
  • print "arrayidx arrayidx\n"

7
Arrays multiple dimensions
  • Arrays can have more than one dimension
  • my _at_gameboard ((" ", " ", " "), (" ", " ", "
    "), (" ", " ", " ") )
  • gameboard 1, 1 "X"
  • gameboard 0, 0 "O"
  • gameboard 1, 2 "X"
  • gameboard 1, 0 "O"

8
Arrays miscellaneous
  • An array can be sorted
  • my _at_sorted sort _at_myarray
  • An array can be reversed
  • my _at_reversed reverse _at_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 hashkey
  • Example phonebook "mangula"

10
Hashes initialization
  • The contents of a hash can be initialized at
    declaration
  • my phonebook (
  • "albert" gt "0787487449",
  • "mangula" gt "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 _at_keys keys myhash
  • The set of values in a hash
  • my _at_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 _at_arr keys hash
  • my length _at_arr
  • my length arr 1
  • Display contents of a hash
  • my _at_keys keys hash
  • for (my idx 0 idx lt _at_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 /(onetwothree)/
  • "one" matches /(one)/, /(on)(e)/
  • group single character options
  • "a" or "b" or "c" match /abc/
  • "af" or "ag" match /abcfgh/
  • "ab" does not match /abcfgh/

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 /aba/
  • ( ) one or more
  • "aa", "abbbb" do not match /aba/

19
Regular Expression pattern syntax continued
  • More quantifiers
  • ( )n exactly n occurrences
  • "afoofoofoob" matches /a(foo)3b/
  • "afoofoob" does not match /a(foo)3b/
  • ( )i, j between i and j occurrences
  • "afoobarbarb" matches /a(foo)1,3(bar)2,4b/
  • "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/
  • /ab/ 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 informationman -M /usr/perl5/man
    perlrequickman -M /usr/perl5/man perlretutman
    -M /usr/perl5/man perlre

23
Review
  • Arrays _at_array ("zero", "one", "two")
    arrayidx array sort _at_array reverse
    _at_array
  • Hashes hash ("a" gt 1, "b" gt 2)hash
    keykeys hash values hash
  • Regular expressions // . ? ()
    ,

24
Next up
  • Process management
  • No class until January
  • See e-mail for exercises
  • e-mail any questions
Write a Comment
User Comments (0)
About PowerShow.com