Log in to users.cs.umb.edu
(NOTE: Parts of this exercise taken from the following page: https://perldoc.perl.org/perldsc.html)
- Log in to a Windows machine
Enter your Unix username as the account name. Then enter your password.- Run the ssh client
Use the values in the table below to connect to users.
ssh configuration parameter value protocol ssh ssh version 2 ssh port 22 host users.cs.umb.edu user name your unix username Authentication method password - Enter your password
A dialog box will appear on the screen into which you must type the password for your Unix account.- Make sure you are in your home directory
cd- Go to your it441 directory
cd it441- Go to your ex directory
cd ex- Create an ex15 directory
mkdir ex15- Go to your ex15 directory
cd ex15- Execute the script command
script- Use your perlsession alias to start the re.pl script.
perlsession(NOTE: If the perlsession alias is not recognized, or if re.pl cannot run, then double-check your Perl configuration, as well as your login shell vs. subshell environments!)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> exitEnd the script session
Use the exit command. You should then have a file called typescript in your current working directory.
(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" ; } }