IT 441: Network Services Administration
Class #10 Exercise

Complete by: 3/30/2018


Log in to users.cs.umb.edu

Array operations

perl>  @months = ("January", "February") ;

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  push @months, "March";

perl>  push @months, "April";

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  push @months, "May";

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  print (shift @months) ;

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  print (shift @months) ;

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  unshift @months, "June";

perl>  unshift @months, "July";

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  print (pop @months) ;

perl>  foreach $m (@months) { print $m ; print "\n"; }

perl>  print (pop @months) ;

perl>  foreach $m (@months) { print $m ; print "\n"; }
(Take note of what push, pop, shift, and unshift are doing...)

Other array usage

perl>  print "Enter the number of the month you were born: "; chomp ($month = ) ;

perl>  print "Enter year you were born: "; chomp ($year = ) ;

perl>  @months = qw/January February March April May June July August September October November December/ ;

perl>  @numDays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) ;

perl>  $numDaysCorrect = $numDays[$month-1] ;

perl>  if ($month==2 && $year%4==0) { $numDaysCorrect = 29 ; }

perl>  print ("$months[$month-1], $year has $numDaysCorrect days \n") ;

Array sorting

perl>  @words = ("decode", "paradise", "wonder", "absorbable", "charm", "ambulance", "fighting", "artificial", "playtime", "disqualified") ;

perl>  foreach $word (@words) { print $word ; print "\n"; }

perl>  @nums = (63, 26, 34, 72, 46, 83, 89, 52, 45, 24) ;

perl>  foreach $val (@nums) { print $val ; print "\n"; }

perl>  @sorted_nums_1 = sort @nums ;

perl>  foreach $val (@sorted_nums_1) { print $val ; print "\n"; }

perl>  @sorted_nums_2 = sort { $a <=> $b } @nums ;

perl>  foreach $val (@sorted_nums_2) { print $val ; print "\n"; }

End the Perl Session

perl>  exit

End the script session

Use the exit command. You should then have a file called typescript in your current working directory.