IT 441: Network Services Administration
Class #09 Exercise

Complete by: 3/26/2018


Log in to users.cs.umb.edu

Simple array printing

perl>  @nums = (1, 2, 3, 4) ;

perl>  print @nums ;

perl>  @nums2 = ("1 ", "2 ", "3 ", "4.") ;

perl>  print @nums2 ;

perl>  $a = 35 ; 

perl>  $b = 7 ; 

perl>  $c = 2 ;

perl>  @nums3 = ($a, $b, $c) ;

perl>  print @nums3 ;

perl>  print join (" ", @nums3) ;

perl>  print join (" ", @nums3) . ".";

perl>  @nums4 = ("$a", '$b', "$c") ;

perl>  print @nums4 ;

perl>  print join (", ", @nums4) ;

perl>  print join (', ', @nums4) . '!' ;

perl>  @nums5 = ( 1 + $a , $b * $c, (1 + $a) / ($b - 1) ) ;

perl>  print @nums5 ;

perl>  print join ("\n", @nums5) ;

perl>  print join ('\n', @nums5) ;
(Think about the variations in output when lines of code looked fairly similar, with only slight differences...
What do you think accounts for those differences?)

Other array printing

perl>  @nums = qw/1, 2, 3, 4, 5, 6, 7/ ;

perl>  print @nums ;

perl>  @nums2 = qw/1 2 3 4 5 6 7/ ;

perl>  print @nums2 ;

perl>  print join (" " , @nums2) ;

perl>  print join (" " , @nums2) . "..." ;

perl>  foreach $num (@nums2) { print "$num\n" ; }

perl>  foreach $num (@nums2) { print '$num\n' ; }

perl>  @nums3 = (1..7) ;

perl>  print join (" " , @nums3) . "..." ;

perl>  foreach $num (@nums3) { print "$num\n" ; }

perl>  print join (" " , (reverse @nums3)) . "!!!" ;

perl>  foreach $num (reverse @nums3) { print "$num...\n" ; }
(Did any of the results seem odd or anomalous?
What do you think accounts for those anomalies?)

Individual elements and array slices

perl>  @nums = (1..10) ;

perl>  print $nums[0] ;

perl>  for ($i = 0 ; $i < 10 ; $i++) { print $nums[$i] ; print "\n"; }

perl>  for ($i = 9 ; $i >= 0 ; $i--) { print ( $nums[$i], "\n" ) ; }

perl>  print ( @nums[0..3] ) ;

perl>  print ( @nums[1..4] ) ;

perl>  print ( @nums[4..1] ) ;

perl>  print ( reverse @nums[1..4] ) ;

perl>  @nums2 = @nums[3..8] ;

perl>  print join (" " , @nums2) . "..." ;

perl>  foreach $num (@nums2) { print "$num\n" ; }

perl>  @nums3 = reverse @nums2 ;

perl>  print join (" " , @nums3) . "..." ;

perl>  foreach $num (@nums3) { print "$num\n" ; }

perl>  $nums[0] = 19;

perl>  $nums[3] = -11;

perl>  $nums[7] = "foobar";

perl>  foreach $value (@nums) { print "$value\n" ; }
(How many results conformed to your predictions? How many did not?
What do you think accounts for those differences?)

Other array operations

perl>  $len = scalar @nums ;

perl>  print 'The length of @nums is: ' . $len ;

perl>  @nums4 = (1..10) ;

perl>  while ((scalar @nums4) > 0) { print shift @nums4 ; print "\n" ; }

perl>  print @nums4;

perl>  @nums5 = (1..10) ;

perl>  while ((scalar @nums5) > 0) { print pop @nums5 ; print "\n" ; }

perl>  print @nums5;
(How many results conformed to your predictions? How many did not?
What do you think accounts for those differences?
What do you think "scalar" does with the array @nums?)

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.