package ArrayUtils; use Exporter; @ISA = qw( Exporter ); use FindArrayMax qw( &find_max_val &find_max_row ); @EXPORT = qw( &load_array &find_max_val &find_max_row ); # if this is commented out, only fully qualified calls work @EXPORT_OK = qw( load_array find_max_val find_max_row ); sub load_array { print "ArrayUtils::load_array\n"; my $fh = shift; my @array; while (<$fh>) { # print "read from file $_"; my @values = split; push @array, \@values; } return \@array; } 1; package ArrayUtils; use Exporter; @ISA = qw( Exporter ); @EXPORT_OK = qw( &find_max_val &find_max_row ); sub find_max_val { print "ArrayUtils::find_max_val\n"; my $matrix = shift; my $max_val; foreach my $row (@{$matrix}) { foreach my $val (@{$row}) { $max_val = $val if ($val > $max_val); } } return $max_val; } sub find_max_row { print "ArrayUtils::find_max_row\n"; my $matrix = shift; my $max_row; my $max_val; foreach my $row (@{$matrix}) { foreach my $val (@{$row}) { $max_row = $row, $max_val = $val if ($val > $max_val); } } return $max_row; } 1; package HashDispatch; use Exporter; @ISA = qw( Exporter ); @EXPORT = qw( &dispatch_table ); my $DEBUG = 1; print "HashDispatch::DEBUG set\n" if ($DEBUG); sub dispatch_table { print "HashDispatch::dispatch_table\n" if ($DEBUG); my $dispatch = { 'stooge' => \&stooges , 'marx' => \&marxes }; return $dispatch; } sub stooges { my ($name, $team) = @_; return "$name, last name is $team->{'stooge'}->{$name}"; } sub marxes { my ($name, $team) = @_; return "$name, real name is $team->{'marx'}->{$name}"; } 1; # my $dispatch = { # 'stooge' => \sub { my ($name, $hash) = @_; print "stooge name is $name\n"; } , # 'marx' => \sub { my ($name, $hash) = @_; print "marx name is $name\n"; } # }; package HashUtils; use FileHandle; use Exporter; @ISA = qw( Exporter ); use HashDispatch; @EXPORT = qw( &load_hash &dispatch_table ); # @EXPORT_OK = qw( &load_hash &dispatch_table ); my $DEBUG = 1; print "HashUtils::DEBUG set\n" if ($DEBUG); sub load_hash { print "HashUtils::load_hash\n" if ($DEBUG); my $fh = shift; my %hash; while (<$fh>) { print "line is $_" if ($DEBUG); my ($team, $first, $second) = split; $hash{$team}->{$first} = $second; } return \%hash; } 1; 4 11 7 9 15 6 2 1 9 use strict; use FileHandle; use ArrayUtils; my $filename = "array-data.txt"; my $fh = FileHandle->new(); $fh->open("< $filename"); my $matrix = load_array($fh); foreach my $row (@{$matrix}) { foreach my $val (@{$row}) { print "$val, "; } print "\n"; } my $max_val = find_max_val($matrix); print "max val is $max_val\n"; my $max_row = find_max_row($matrix); print "max row is: "; foreach my $val (@{$max_row}) { print "$val, "; } print "\n"; use strict; use FileHandle; use ArrayUtils qw( &load_array &find_max_val &find_max_row ); my $filename = "array-data.txt"; my $fh = FileHandle->new(); $fh->open("< $filename"); my $matrix = ArrayUtils::load_array($fh); foreach my $row (@{$matrix}) { foreach my $val (@{$row}) { print "$val, "; } print "\n"; } my $max_val = ArrayUtils::find_max_val($matrix); print "max val is $max_val\n"; my $max_row = ArrayUtils::find_max_row($matrix); print "max row is: "; foreach my $val (@{$max_row}) { print "$val, "; } print "\n"; marx groucho julius stooge moe howard stooge larry fine marx chico leonard marx harpo arthur stooge curly howard use strict; use Data::Dumper; use FileHandle; use HashUtils; my $filename = "hash-data.txt"; my $fh = FileHandle->new(); $fh->open("< $filename"); my $team = load_hash($fh); print Dumper $team; my $table = dispatch_table(); foreach my $team_name (keys(%{$team})) { print "team name is $team_name\n"; foreach my $name (keys(%{ $team->{$team_name} })) { my $result = $table->{$team_name}->($name, $team); print "$result\n"; } } use strict; use Data::Dumper; use FileHandle; use HashUtils qw( &load_hash &dispatch_table ); my $filename = "hash-data.txt"; my $fh = FileHandle->new(); $fh->open("< $filename"); my $team = HashUtils::load_hash($fh); print Dumper $team; my $table = HashUtils::dispatch_table(); foreach my $team_name (keys(%{$team})) { print "team name is $team_name\n"; foreach my $name (keys(%{ $team->{$team_name} })) { my $result = $table->{$team_name}->($name, $team); print "$result\n"; } } use strict; use Data::Dumper; use FileHandle; use HashUtils; my $DEBUG = 1; print "main::DEBUG set\n" if ($DEBUG); my $filename = "hash-data.txt"; my $fh = FileHandle->new(); $fh->open("< $filename"); my $team = load_hash($fh); print Dumper $team; my $table = dispatch_table(); foreach my $team_name (keys(%{$team})) { print "team name is $team_name\n"; foreach my $name (keys(%{ $team->{$team_name} })) { my $result = $table->{$team_name}->($name, $team); print "$result\n"; } }