IT 441: Network Services Administration
Class #15 Exercise

Complete by: 4/20/2018


Log in to users.cs.umb.edu

(NOTE: Parts of this exercise taken from the following page: https://perldoc.perl.org/perldsc.html)

Arrays of arrays - What will not work

perl>  @array_1 = ( "fred", "barney" ) ;

perl>  @array_2 = ( "george", "jane", "elroy" ) ;

perl>  @array_3 = ( "homer", "marge", "bart" ) ;

perl>  @array_of_arrays = ( @array_1 , @array_2 , @array_3 ) ;

perl>  print "Array of arrays length is: " . scalar (@array_of_arrays) . "\n" ;

perl>  foreach $value (@array_of_arrays) { print "$value\n" ; }

Arrays of arrays - What will work

perl>  @array_of_arrays = ( [ @array_1 ], [ @array_2 ], [ @array_3 ] );

perl>  print "Array of arrays length is: " . scalar (@array_of_arrays) . "\n" ;

Accessing sub-arrays - What will and will not work

perl>  print "$array_of_arrays[0]\n" ;

perl>  print "@array_of_arrays[0]\n" ;

perl>  print "@{ $array_of_arrays[0] }\n" ;

perl>  print "First sub-array length is " . scalar (@{ array_of_arrays[0] }) ;

perl>  print "@{ $array_of_arrays[1] }\n" ;

perl>  print "@{ $array_of_arrays[2] }\n" ;

perl>  foreach $inner_array (@array_of_arrays) { print "$inner_array\n" ; }

perl>  foreach $inner_array (@array_of_arrays) { print "@{ $inner_array }\n" ; }

Printing our arrays of arrays

perl>  print "Array of arrays length: " . scalar (@array_of_arrays) . "\n" ; foreach $inner_array (@array_of_arrays) { @arr = @{ $inner_array } ; print "Sub-array length is " . scalar (@arr) . " - " . join (", " , @arr) . "\n" ; }

Encapsulate previous inside a subroutine

perl>  sub arr_display { print "Array of arrays length: " . scalar (@array_of_arrays) . "\n" ; foreach $inner_array (@array_of_arrays) { @arr = @{ $inner_array } ; print "Sub-array length is " . scalar (@arr) . " - " . join (", " , @arr) . "\n" ; } }

Alterations to outer vs. inner arrays - pay attention to the difference!

perl>  push @{ $array_of_arrays[0] } , "wilma"; push @{ $array_of_arrays[0] } , "betty";

perl>  arr_display();

perl>  pop @{ $array_of_arrays[1] } ; pop @{ $array_of_arrays[1] } ; 

perl>  arr_display();

perl>  push @array_of_arrays, [ "peter", "lois", "brian", "chris", "meg" ];

perl>  arr_display();

perl>  shift @array_of_arrays; 

perl>  arr_display();

perl>  unshift @{ $array_of_arrays[0] }, "rosie";

perl>  arr_display();

perl>  unshift @array_of_arrays, [ "eric", "kyle", "cartman", "kenny", ];

perl>  arr_display();

Hash of arrays

perl>  %hash_of_arrays = ( 'flintstones', [ "fred", "barney" ], 'jetsons', [ "george", "jane", "elroy" ], 'simpsons', [ "homer", "marge", "bart" ] ) ;

perl>  foreach $key (keys(%hash_of_arrays)) {  print "$key\n"; }

perl>  while (($key, $value) = each (%hash_of_arrays)) { print "$key: $value\n" ; }

perl>  while (($key, $value) = each (%hash_of_arrays)) { print "$key: @{ $value }\n" ; }

perl>  print "$hash_of_arrays{flintstones}\n" ;

perl>  print "@hash_of_arrays{flintstones}\n" ;

perl>  print "@{ $hash_of_arrays{flintstones} }\n" ;

perl>  print "\"flintstones\" array length is " . scalar (@{ $hash_of_arrays{flintstones} }) ;

perl>  @jetsons_array = @{ $hash_of_arrays{jetsons} };

perl>  foreach $value (@jetsons_array) { print "$value\n"; } 

Hash display subroutine

perl>  sub hash_display { print "Keys in hash of arrays: " . join (", ", keys (%hash_of_arrays)) . "\n\n" ; while (($key, $value) = each (%hash_of_arrays)) { @arr = @{ $value } ; print "\"$key\" array length is " . scalar (@arr) . " - " . join (", " , @arr) . "\n" ; } }

Alterations to the hash vs. the inner arrays - pay attention to the difference!

perl>  push @{ $hash_of_arrays{flintstones} } , "wilma"; push @{ $hash_of_arrays{flintstones} } , "betty";

perl>  hash_display();

perl>  pop @{ $hash_of_arrays{jetsons} } ; pop @{ $hash_of_arrays{jetsons} } ; 

perl>  hash_display();

perl>  $hash_of_arrays{'griffins'} = [ "peter", "lois", "brian", "chris", "meg" ];

perl>  hash_display();

perl>  delete $hash_of_arrays{flintstones}; 

perl>  hash_display();

perl>  unshift @{ $hash_of_arrays{jetsons} }, "rosie";

perl>  hash_display();

perl>  $hash_of_arrays{'southpark'} = [ "eric", "kyle", "cartman", "kenny", ];

perl>  hash_display();

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.

Subroutines with neater formatting

(Code in exercise above lacks newlines because of perlsession. This is how the code could look with neater formatting.)
sub arr_display {

    print "Array of arrays length: " . scalar (@array_of_arrays) . "\n" ;
    foreach $inner_array (@array_of_arrays) {

        @arr = @{ $inner_array } ; 
        print "Sub-array length is " . scalar (@arr) . " - " . join (", " , @arr) . "\n" ;

    } 

}

sub hash_display {

    print "Keys in hash of arrays: " . join (", ", keys (%hash_of_arrays)) . "\n\n" ;
    while (($key, $value) = each (%hash_of_arrays)) {

        @arr = @{ $value } ; 
        print "\"$key\" array length is " . scalar (@arr) . " - " . join (", " , @arr) . "\n" ; 

    } 

}