Perl Lesson 4

Document Sample
Perl Lesson 4 Powered By Docstoc
					  Perl                             Lesson 4


        courses@xfzhou.homedns.org



                                     1




                 RE Rule 7
• ?=<RE>,
• ?!<RE>               match
• ?<=<RE>, lood behind
  – "cat housecat catch crazycats"
  – /(?<=\s)cat(?=\s)/         /(?<!\s)cat(?=\s)/
• ?:<RE>,
• xims
VH\L" UHZVQ$        FWH 6(< VH\
• ?#comment, replaced by m/…/x now.
                                     2




                                                    1
              Quote-like operators
 •   ?RE?         Like m/ /, match only once.
 •   m/ /, s///, tr///, y///
 •   q/string/ 'string'               qq/str/ "str"
 •   qx/command/ `command`
 •   qw/string/ split(' ', q/string/)
 •   HERE document (like those in UNIX shell scripts)
      print << "HERE";
      Many lines here with interplation
      HERE
      print << 'THERE';
      Many lines here but no interplation
      THERE
                                            3




     join, map, split, reverse, sort, grep
join("\n", @array);                  # return string
map("$_\n", @array);                 # return array
     print join "\t", map $_*$_, 1..10;
     print join "\t", map int rand(20), 1..10;
split(/pattern/, "string");          # return array
     @list = split(//, "ABCDEFGH");
     @list = ("ABCDEFGH" =~ /(.{1,3})/g);
reverse(@array);                     # return array
sort(@array);                        # return array
     sort {$a cmp $b} @list;
     sort {$a <=> $b} @list;
     sort {-($a <=> $b)} @list;
grep(/pattern/, @array);             # return filtered array
     @foo = grep(!/^#/, @bar);


                                            4




                                                               2
           Hash (associative array)--%
• Index (or key) is an scalar (usually, string).
• Order less.
• Relational database
                                     HQX-  HR-
                         FH'  NFD-  UU$RVVD            EH)  HQD-
                                     HQX- ! HR-
                         FH' ! NFD-  UU$RVVD            EH) ! HQD-
                        WF2    ` \UUD0 ^UU$RVVD
                     UD0    ` UHILQQH- ^UU$RVVD
  ` % HR- ^UU$RVVD  VL \DGKWULE V
HR- WQLUS               Q?
 =>                      HQX- VL \DGKWULE V
HR-
                     KVDK \WSPH      UU$RVVD

 %hex2bin = (0 => "0000", 1 => "0001", ... , F=>"1111");
 $hex = "A19e";
 $bin = join("_",map($hex2bin{$_},
                     split(//,uc $hex)));
 =>1010_0001_1001_1110

                                                     5




       Functions working on HASH, 1
      • Nevery say print %hash; since hash is orderless.
      • @key = sort keys %hash
         – foreach $key (keys %hash) {print $hash{$key};}
      • @key = keys %hash; @value = values %hash;
      • ($key, $val) = each(%hash)             # less memory
      each( ) returns once a pair, last time, returns undef.
      print "$key=$value\n" while (($key,$value) = each
        %ENV); # mean while, never modify %ENV !!!
      =>. . .
        TZ=BJT-8
        PROCESSOR_REVISION=0806
        NUMBER_OF_PROCESSORS=1
        USERPROFILE=C:\WINNT\Profiles\xfzhou
        TMPDIR=c:/temp
        COMPUTERNAME=N30
        . . .                            6




                                                                            3
    Functions working on HASH, 2
• Remove one hash from hash table
    – delete $hash{$key};
    – This is wrong: undef $hash {key};
• Delete a whole hash table
    – %hash = ();
    – undef %hash;
• Check existence of a hash
    – if (exists $hash{$key}) {….}
    – This is wrong: if ($hash{$key}) {….}
    – This is also wrong: if (defined $hash{$key}) …
                                     7




       Special variable 1-- perlvar
•   $_      Default parameter for many funcs
•   @_      parameter list passed to sub routine
•   Pid:          $$
•   Command line: @ARGV
•   Environments: %ENV
    foreach $key (keys(%ENV))
      {print "$key\t$ENV{$key}\n";}
• Signal handling: %SIG
       $SIG{"HUP"} = "IGNORE";
• Program name: $0
• Pattern memory:$1, $2, …, $+ (last bracket matches)
• "ABCDEFG" =~ /CD/; print " $` - $& - $' ";
                                     8




                                                        4