perlhb.doc 5/12/98

       
Regular Expressions - Matching, Substitute, Split
loops
Sorting
 
anchors

naked blocks

subroutines / functions
 
preset patterns
Control Structures:
Executing external commands from within a Perl program
 
parts of matched expression
arrays
file management
 
Transliterate
associative arrays
Splitting and joining review
 
<STDIN>
|| die ("mesage here")
 
 
 

$_ || && ?

$lineRead = <STDIN>;

Regular Expressions - Matching, Substitute, Split

anchors ^ $ \b \B

=~ /a/ /^start of line/ /end of line$/

= /\bat start of word/ /at end of word\b/

+ ? * (n,n) + = 1 or more, * = 0 or more ?

. = any char

/\W.*/ = all characters on line after first word

$a = s/\W.*// #leaves only first word

preset patterns \d \D \w \W

$a = s/(\w+)\W+(\w+)/\2 \1/ #exchanges first word with second

and $1 = value in \1 in expression, $2 = value in \2 etc.

if ($a = /^$/) ... #blank line

$_ = "this is the default string for some operations

($a,$b) = /(\w+)\W+(w+)/; #assumes string is in $_

$a = s/ //; #replaces first blank in string

$a = s/ //g; #replaces ALL blanks in string

if ($answer =~ /no/) { # if lower case "no" in string

if ($answer -~ /no/i} { # if "no" in string, case ignored

$a =~ /(ax|bx|cx)/;

if ($a =~ /[song|blue]bird/) {... }

parts of matched expression

$a =~ /\t/; # then $` = all before match,

$& = matched part,

$' = after match

Transliterate

tr/a-z/A-Z/ # make everything upper case in $_

$a = tr/abcd/ABCD/ #replaces ?? Go learn

tr/abcd/xy/d tr/abcd/xy/s ??

loops

while, until, for

next, last, redo

while (a$=<INFILE>) {
                    ...
                    if (... ) last;
                    ...
                    }

this_is_name:until (i > 10) {
                    ...
                    while ...{
                    ...
                    if (.. ) last this_is_name;
                    ...
                    }

naked blocks

last (next? redo? )

{ #this is a naked block, not part of a loop or if statement
...
if ( $a > $b ) last;
}

named_naked_block:{

...
if ( ... ) {last named_naked_block};
...
}

Control Structures:

if (... ) { .. }

print "this should work" if (... );

|| && ?

= < >

eq

arrays

@a = ($a, $z, $var);

@a = (@b,@c);

($a1,$a2,$a3) = @a;

@b = ($a[3],$a[2],$a[1]);

foreach $var (@a) {

foreach $val (sort (@a)) {

associative arrays

@keys = key(%my_array);

@values = value(%my_array);

foreach $var2 (key(%my_array) {

val1 = my_array[$var2];

foreach $var2 (sort(key(%my_array)) {

val1 = my_array[$var2];

Sorting

@a = sort(@b);

@a = sort by_function (@b);

@a = sort by_another_function (@b);

re-study, fill in ... how to create your own sort function

subroutines / functions

functions can appear any where - convention is at end of program

sub this_function {
                  ...
                  }

arguments are passed in as an an array of scalars

sub function2 {

@arguments = @$_; ??

or arg1 = $_[0]; # check this

local (a$,b$,c$... )

last value computed is the return value of the subroutine

Executing external commands from within a Perl program

system ("dir a*.*"); #output to screen;

a$ = `dir a*.*`; #output goes in to a$

Using file handle as command

open (FILECOM,"dir a*.*|"); #see book about this.. interesting

file management

how to test file attributes : exists? read/write? etc.

open (FILEHANDLE,"c:\temp.txt");

open (INF,"patin.txt") || die ("could not find the sucker");

close (INF);

while ($inf = <INF>)  {
                      ...
                      }

open (OUTF,">outfile.txt");

open (OUTF,">>outfile.txt");

Globbing

while ($file = <in*.txt>) {

open (INF,$file) || die "could not open it";

while ($n = <INF>) { ... }

Splitting and joining review

@b = split (" ",$a);

@b = split (":",$a);

$a = join (":",@a);