# 3. CallMain1.pl package CallMain1; # This version imports the subs with explict args to use command use strict; use Data::Dumper; use NewArray1 qw( loadarr printarr); my $data_file= "data.txt"; my $AofA = loadarr($data_file); my ($aref, $hold) = printarr($AofA); print "\nThe row with the largest number is: \n" ; print Dumper $aref; print "\nThe largest number is: \n" ; print Dumper $hold; print "\nProgram Complete: Array of Array's \n" ; use NewHash1 qw( loadhash printhash); my $hdata_file= "hdata.txt"; my $HofH = loadhash($hdata_file); printhash($HofH); # 4. NewArray1.pm package NewArray1; use strict; use Data::Dumper; use base 'Exporter'; use vars '@EXPORT_OK'; @EXPORT_OK = qw( loadarr printarr ); sub loadarr { my ($fh) = @_; open DAT, "$fh" || die "Could not open file! ($!)"; print "\nProgram Homework1: Array of Array's \n" ; my $AofA ; my $hold = 0; my $aref; while (my $line = <DAT>) { push @{$AofA}, [split(" ", $line)]; } print "\nThe Array of Array's contains: \n" ; print Dumper @{$AofA}; return ($AofA); } sub printarr { my $hold; my $ele; my $aref; my ($AofA) = @_; foreach my $Arr ( @{$AofA} ) { foreach my $ele ( @{$Arr} ) { if ($ele > $hold) { $aref = $Arr; $hold = $ele; } } } return ($aref, $hold); } # 5. NewHash1.pm package NewHash1; use strict; use Data::Dumper; use base 'Exporter'; use vars '@EXPORT_OK'; @EXPORT_OK = qw( loadhash printhash ); sub loadhash { my ($fh) = @_; open DAT, "$fh" || die "Could not open file! ($!)"; print "\nProgram Homework1: Two Level Hash \n\n" ; my $HofH; my $team; my $last; my $first; while (my $line = <DAT>) { ($team, $first, $last) = split(" ", $line); $HofH->{$team}{$first} = $last; } print Dumper %{$HofH}; return ($HofH); } sub printhash { my ($HofH) = @_; my %dispatch = ( 'stooge' => \&stooge, 'marx' => \&marx, 'more' => \&more, ); foreach my $team_name (keys %{$HofH}) { print "\nComedy Team: $team_name\n" ; my $code_ref = $dispatch{ $team_name }; $code_ref or die "Can't find $team_name in dispatch table" ; foreach my $first_name (sort keys %{ $HofH->{$team_name} } ) { my $last_name = $HofH->{$team_name}->{$first_name} ; print "\nFirst Name : $first_name \n" ; $code_ref->($first_name); } } sub stooge { my $nameref = shift; print "Last Name : $HofH->{'stooge'}->{$nameref}\n"; } sub marx { my $nameref = shift; print "Last Name : $HofH->{'marx'}->{$nameref}\n"; } sub more { print Dumper "not found"; } } # 3. CallMain2.pl package CallArray2; # This version imports the subs with an %EXPORT_TAGS tag use strict; use Data::Dumper; use NewArray2 qw( :Both); my $data_file= "data.txt"; my $AofA = loadarr($data_file); my ($aref, $hold) = printarr($AofA); print "\nThe row with the largest number is: \n" ; print Dumper $aref; print "\nThe largest number is: \n" ; print Dumper $hold; print "\nProgram Complete: Array of Array's \n" ; use NewHash2 qw( :Both); my $hdata_file= "hdata.txt"; my $HofH = loadhash($hdata_file); printhash($HofH); # 4. NewArray2.pm package NewArray2; use strict; use Data::Dumper; use base 'Exporter'; use vars qw '@EXPORT_OK %EXPORT_TAGS'; @EXPORT_OK = qw( loadarr printarr ); %EXPORT_TAGS = ( Both =>[qw(loadarr printarr)]); sub loadarr { my ($fh) = @_; open DAT, "$fh" || die "Could not open file! ($!)"; print "\nProgram Homework2: Array of Array's \n" ; my $AofA ; my $hold = 0; my $aref; while (my $line = <DAT>) { push @{$AofA}, [split(" ", $line)]; } print "\nThe Array of Array's contains: \n" ; print Dumper @{$AofA}; return ($AofA); } sub printarr { my $hold; my $ele; my $aref; my ($AofA) = @_; foreach my $Arr ( @{$AofA} ) { foreach my $ele ( @{$Arr} ) { if ($ele > $hold) { $aref = $Arr; $hold = $ele; } } } return ($aref, $hold); } # 5. NewHash2.pm package NewHash2; use strict; use Data::Dumper; use base 'Exporter'; use vars qw '@EXPORT_OK %EXPORT_TAGS'; @EXPORT_OK = qw( loadhash printhash ); %EXPORT_TAGS = ( Both =>[qw(loadhash printhash)]); sub loadhash { my ($fh) = @_; open DAT, "$fh" || die "Could not open file! ($!)"; print "\nProgram Homework2: Two Level Hash \n\n" ; my $HofH; my $team; my $last; my $first; while (my $line = <DAT>) { ($team, $first, $last) = split(" ", $line); $HofH->{$team}{$first} = $last; } print Dumper %{$HofH}; return ($HofH); } sub printhash { my ($HofH) = @_; my %dispatch = ( 'stooge' => \&stooge, 'marx' => \&marx, 'more' => \&more, ); foreach my $team_name (keys %{$HofH}) { print "\nComedy Team: $team_name\n" ; my $code_ref = $dispatch{ $team_name }; $code_ref or die "Can't find $team_name in dispatch table" ; foreach my $first_name (sort keys %{ $HofH->{$team_name} } ) { my $last_name = $HofH->{$team_name}->{$first_name} ; print "\nFirst Name : $first_name \n" ; $code_ref->($first_name); } } sub stooge { my $nameref = shift; print "Last Name : $HofH->{'stooge'}->{$nameref}\n"; } sub marx { my $nameref = shift; print "Last Name : $HofH->{'marx'}->{$nameref}\n"; } sub more { print Dumper "not found"; } } # 3. CallMain3.pl package CallArray3; # This version will not import but will call the subs with Fully Qualified Names use strict; use Data::Dumper; use NewArray3; my $data_file= "data.txt"; my $AofA = NewArray3::loadarr($data_file); my ($aref, $hold) = NewArray3::printarr($AofA); print "\nThe row with the largest number is: \n" ; print Dumper $aref; print "\nThe largest number is: \n" ; print Dumper $hold; print "\nProgram Complete: Array of Array's \n" ; use NewHash3; my $hdata_file= "hdata.txt"; my $HofH = NewHash3::loadhash($hdata_file); NewHash3::printhash($HofH) 4. NewArray3.pm --- application/octet-stream; NewArray3.pm]... package NewArray3; use strict; use Data::Dumper; sub loadarr { my ($fh) = @_; open DAT, "$fh" || die "Could not open file! ($!)"; print "\nProgram Homework3: Array of Array's \n" ; my $AofA ; my $hold = 0; my $aref; while (my $line = <DAT>) { push @{$AofA}, [split(" ", $line)]; } print "\nThe Array of Array's contains: \n" ; print Dumper @{$AofA}; return ($AofA); } sub printarr { my $hold; my $ele; my $aref; my ($AofA) = @_; foreach my $Arr ( @{$AofA} ) { foreach my $ele ( @{$Arr} ) { if ($ele > $hold) { $aref = $Arr; $hold = $ele; } } } return ($aref, $hold); } return ('TRUE'); # 5. NewHash3.pm package NewHash3; use strict; use Data::Dumper; sub loadhash { my ($fh) = @_; open DAT, "$fh" || die "Could not open file! ($!)"; print "\nProgram Homework3: Two Level Hash \n\n" ; my $HofH; my $team; my $last; my $first; while (my $line = <DAT>) { ($team, $first, $last) = split(" ", $line); $HofH->{$team}{$first} = $last; } print Dumper %{$HofH}; return ($HofH); } sub printhash { my ($HofH) = @_; my %dispatch = ( 'stooge' => \&stooge, 'marx' => \&marx, 'more' => \&more, ); foreach my $team_name (keys %{$HofH}) { print "\nComedy Team: $team_name\n" ; my $code_ref = $dispatch{ $team_name }; $code_ref or die "Can't find $team_name in dispatch table" ; foreach my $first_name (sort keys %{ $HofH->{$team_name} } ) { my $last_name = $HofH->{$team_name}->{$first_name} ; print "\nFirst Name : $first_name \n" ; $code_ref->($first_name); } } sub stooge { my $nameref = shift; print "Last Name : $HofH->{'stooge'}->{$nameref}\n"; } sub marx { my $nameref = shift; print "Last Name : $HofH->{'marx'}->{$nameref}\n"; } sub more { print Dumper "not found"; } } return ('TRUE');