:::::::::::::: README :::::::::::::: There are 24 examples: A. Example Files (These are executable perl scripts unless otherwise noted.): example1 example2 - takes input from input2 example3 example4 example5 - executable shell batch file example6 example7 example8 example9 - takes input from input9 example10 example11 example12 example13 example14 example15 example16 example17 - takes input from input17 example18 - takes input from input18 example19 - takes input from input19 example20 example21 example22 - takes input from input22 example23 example24 B. Other Files: excluder - is called from example5 names - text data file C. Using these examples All of the examples in this chapter are executable scripts that can be run directly at the command line. The ones that take input from a provided input file can be run as follows: > example17 < input17 :::::::::::::: example1 :::::::::::::: #! /usr/bin/perl $num1=1; $num2=0; $str1="hello"; $str2=""; # Null string if ( $num1 ) {print "TRUE!\n"; $x++;} # $x and $y were # initially assigned zero if ( $num2 ) {print "FALSE! \n";$y++;} # never execute this block if ( $str1 ) {print "TRUE AGAIN!\n";} if ( $str2 ) {print "FALSE AGAIN!\n";} print "Not Equal!\n" if $x != $y; :::::::::::::: example10 :::::::::::::: #! /usr/bin/perl $x = 1; do { print "$x "; $x++; } while ($x <= 10); print "\n"; $y = 1; do { print "$y " ; $y++; } until ( $y > 10 ); print "\n"; :::::::::::::: example11 :::::::::::::: #! /usr/bin/perl for ( $i=0; $i<10; $i++ ){ # initialize, test, and increment $i print "$i "; } print "\nOut of the loop.\n"; :::::::::::::: example12 :::::::::::::: #! /usr/bin/perl # Initialization, test, and increment, decrement of # counters is done in one step. for ( $count=1, $beers=10, $remain=$beers, $where="on the shelf"; $count <= $beers; $count++, $remain--){ if($remain == 1){print "$remain bottle of beer $where $where." ;} else{ print "$remain bottles of beer $where $where.";} print " Take one down and pass it all around.\n"; print "Now ", $beers - $count , " bottles of beer $where!\n"; if ( $count == 10 ){print "Party's over.\n";} } :::::::::::::: example13 :::::::::::::: #! /usr/bin/perl foreach $pal ( 'Tom' , 'Dick' , 'Harry' , 'Pete' ) { print "Hi $pal!\n"; } :::::::::::::: example14 :::::::::::::: #! /usr/bin/perl foreach $hour ( 1 .. 24 ){ # The range operator is used here if ( $hour > 0 && $hour < 12){ print "Good-morning.\n"; } elsif ( $hour == 12){ print "Happy Lunch.\n";} elsif ( $hour > 12 && $hour < 17) { print "Good afternoon.\n"; } else { print "Good-night.\n" ; } } :::::::::::::: example15 :::::::::::::: #! /usr/bin/perl $str="hello"; @numbers = ( 1, 3, 5, 7, 9 ); print "The scalar \$str is initially $str.\n"; print "The array \@numbers is initially @numbers.\n"; foreach $str ( @numbers ){ $str+=5; print "$str\n"; } print "Out of the loop--\$str is $str.\n"; print "Out of the loop--The array \@numbers is now @numbers.\n"; :::::::::::::: example16 :::::::::::::: #! /usr/bin/perl @colors=(red, green, blue, brown); foreach ( @colors ) { print "$_ "; $_="YUCKY"; } print "\n@colors\n"; :::::::::::::: example17 :::::::::::::: #! /usr/bin/perl #!//usr/bin/perl # Program that uses a label without a loop and the redo statement ATTEMPT: { print "Are you a great person? "; chomp($answer = ); redo ATTEMPT unless $answer eq "yes" ; } :::::::::::::: example18 :::::::::::::: #! /usr/bin/perl while(1){ # start an infinite loop print "What was your grade? "; $grade = ; if ( $grade < 0 || $grade > 100 ) { print "Illegal choice\n"; next; # start control at the beginning of the innermost loop } if ( $grade > 89 && $grade < 101) { print "A\n"; } elsif ( $grade > 79 && $grade < 90 ) { print "B\n"; } elsif ( $grade > 69 && $grade < 80 ) { print "C\n"; } elsif ( $grade > 59 && $grade < 70 ) { print "D\n"; } else { print "You Failed."}; print "Do you want to enter another grade? (y/n) "; chomp($choice = ); last if $choice ne "y"; # break out of the innermost loop if the condition is true } :::::::::::::: example19 :::::::::::::: #! /usr/bin/perl ATTEMPT:{ print "What is the course number? "; chomp($number = ); print "What is the course name? "; chomp($course = ); $department{$number} = $course; print "\nReady to quit? "; chomp($answer = ); last if "$answer" eq "yes"; redo ATTEMPT; } $number = 101; print "Course 101 is $department{$number}\n" if $number==101; :::::::::::::: example2 :::::::::::::: #! /usr/bin/perl print "What version of the operating system are you using? "; chomp($os=); if ( $os > 2.2 ) { print "Most of the bugs have been worked out!\n";} else { print "Expect some problems.\n";} :::::::::::::: example20 :::::::::::::: #! /usr/bin/perl OUT: while(1){ print "Inside OUT loop with i = ", ++$i ,"\n"; MID: while(1){ if ( $i == 5 ) { last OUT; } print "Inside MID loop with i = ", ++$i ,"\n"; INNER: while(1){ if ( $i == 4 ){ next OUT; } print "Inside INNER loop with i = ", ++$i ,"\n"; } } } print "Out of all loops.\n"; :::::::::::::: example21 :::::::::::::: #! /usr/bin/perl for ( $rows=5; $rows>=1; $rows-- ){ for ( $columns=1; $columns<=$rows; $columns++ ){ printf "*"; } print "\n"; } :::::::::::::: example22 :::::::::::::: #! /usr/bin/perl # This script prints the average salary of employees # earning over $50,000 annually # There are 5 employees. If the salary falls below $50,000 # it is not included in the tally. EMPLOYEE: for ($emp=1,$number=0; $emp <= 5; $emp++ ){ do { print "What is the monthly rate for employee #$emp? "; print "(Type q to quit) "; chomp($monthly=); last EMPLOYEE if $monthly eq 'q'; next EMPLOYEE if (($year=$monthly * 12.00) <= 50000); $number++; $total_sal += $year; next EMPLOYEE; } while($monthly ne 'q'); } unless($number == 0){ $average = $total_sal/$number; print "There were $number employees who earned over \$50,000 annually.\n"; printf "Their average annual salary is \$%.2f.\n", $average; } else { print "None of the employees made over \$50,000\n"; } :::::::::::::: example23 :::::::::::::: #! /usr/bin/perl # # Example using the continue block # for ($i=1; $i<=10; $i++) { # $i is incremented only once if ($i==5){ print "\$i == $i\n"; next; } print "$i "; } print "\n"; print '=' x 35; print "\n"; # --------------------------------------------------------- $i=1; while ($i <= 10){ if ($i==5){ print "\$i == $i\n"; $i++; # $i must be incremented here or an infinite loop will start next; } print "$i "; $i++; } print "\n"; print '=' x 35; print "\n"; # --------------------------------------------------------- $i=1; while ($i <= 10){ if ($i==5){ print "\$i == $i\n"; next; } print "$i "; } continue { $i++; } print "\n"; :::::::::::::: example24 :::::::::::::: #! /usr/bin/perl $hour=0; while($hour < 24) { SWITCH: { # SWITCH is just a user-defined label $hour =~ /^[0-9]$|^1[0-1]$/ && do { print "Good-morning!\n"; last SWITCH;}; $hour == 12 && do {print "Lunch!\n"; last SWITCH;}; $hour =~ /1[3-7]/ && do {print "Siesta time!\n"; last SWITCH;}; $hour > 17 && do {print "Good night.\n"; last SWITCH;}; } # End of block labeled SWITCH $hour++; } # End of loop block :::::::::::::: example3 :::::::::::::: #! /usr/bin/perl $hour=` date +%H` ; if ( $hour >= 0 && $hour < 12 ){print "Good morning!\n";} elsif ($hour == 12 ){print "Lunch time.\n";} elsif ($hour > 12 && $hour < 17 ) {print "Siesta time.\n";} else {print "Goodnight. Sweet dreams.\n";} :::::::::::::: example4 :::::::::::::: #! /usr/bin/perl $num1=1; $num2=0; $str1="hello"; $str2=""; # Null string unless ( $num1 ) {print "TRUE!\n"; $x++;} # never execute this block unless( $num2 ) {print "FALSE!\n"; $y++;} unless ( $str1 ) {print "TRUE AGAIN!\n";} unless( $str2 ) {print "FALSE AGAIN!\n";} print "Not Equal!\n" unless $x == $y; # unless modifier and simple statement :::::::::::::: example5 :::::::::::::: #! /bin/sh PATH='$PATH:.' excluder names :::::::::::::: example6 :::::::::::::: #! /usr/bin/perl $num=0; #initialize $num while($num < 10 ){ # test expression; # loop quits when expression is false or 0 print "$num "; $num++; # update the loop variable $num; increment $num } print "\nOut of the loop.\n"; :::::::::::::: example7 :::::::::::::: #! /usr/bin/perl $count=1; #Initialize variables $beers=10; $remain=$beers; $where="on the shelf"; while($count <= $beers) { if($remain == 1){print "$remain bottle of beer $where " ;} else{ print "$remain bottles of beer $where $where ";} print "Take one down and pass it all around.\n"; print "Now ", $beers - $count , " bottles of beer $where!\n"; $count++; $remain--; if ( $count > 10 ){print "Party's over. \n";} } print "\n"; :::::::::::::: example8 :::::::::::::: #! /usr/bin/perl $num=0; #initialize until ($num == 10 ){ # test expression; loop quits when expression is true or 1 print "$num "; $num++; # update the loop variable $num; increment $num } print "\nOut of the loop.\n"; :::::::::::::: example9 :::::::::::::: #! /usr/bin/perl print "Are you o.k.? "; chomp($answer=); until ($answer eq "yes" ){ sleep(1); print "Are you o.k. yet? "; chomp($answer=); } print "Glad to hear it!\n"; :::::::::::::: excluder :::::::::::::: #! /usr/bin/perl # Program name: excluder while(<>){ ($name, $phone)=split(/:/); unless($name eq "barbara"){ $record="$name\t$phone"; print "$record"; } } print "\n$name has moved from this district.\n"; :::::::::::::: input17 :::::::::::::: Nope Sometimes yes :::::::::::::: input18 :::::::::::::: 94 y 66 n :::::::::::::: input19 :::::::::::::: 101 CIS342 n 201 BIO211 n 301 ENG120 yes :::::::::::::: input2 :::::::::::::: What version of the operating system are you using ? 2.0 Expect some problems. The \i if/elsif/else \i0 Construct. Yet another form of the \i if \i0 statement is the \i if/else/elsif \i0 con-struct. This construct provides a multi-way decision structure. If the first conditional expression following the \i if \i0 keyword is \i tru\i0 e, the block of statements following the \i if \i0 is exe-cuted. Otherwise, the first \i elsif \i0 statement is tested. If the conditional expression following the first \i elsif \i0 is \i fals\i0 e, the next \i elsif \i0 is tested, etc. If all of the conditional expressions follow-ing the \i elsif ' s \i0 are \i fals\i0 e, the block after the \i else \i0 is executed; this is the default action. :::::::::::::: input22 :::::::::::::: 4000 5500 6000 3400 4500 :::::::::::::: input9 :::::::::::::: n nope yup yes :::::::::::::: names :::::::::::::: igor chevsky:408-123-4533 paco gutierrez:510-453-2776 ephram hardy:916-235-4455 james ikeda:415-449-0066 barbara kerz:207-398-6755 jose santiago:408-876-5899 tommy savage:408-876-1725 lizzy stachelin:415-555-1234 barbara:510-558-9041