I'm glad you found the solution useful. Perl 5.10 isn't required for the logic presented.
As choroba correctly notes, say is the only 5.10 feature used: he's provided two workarounds for older Perls; a third option is to add -l to the shebang line and just replace all instances of say with print without needing to add "\n" to each (see perlrun for details). My personal preference would be for the print ..., "\n"; option: the sub say {...} option may cause some confusion if this code is revisited at some future time; -l may cause problems if you later want to add a print statement that doesn't require a terminating newline.
I've also noted that the scope of %cross is the entire script but it's only used by sub crosslink {...}. To avoid accidently modifying that hash in some other part of the code, you can hide it from everything except that subroutine with:
{
my %cross;
sub crosslink {
...
}
}
Putting all that together, here's an improved version that should work in almost any version of Perl 5 (I certainly don't see anything that wouldn't work in 5.8.8).
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw{first};
use constant {
HOUSE => 0,
FAMILY => 1,
EXTERN => 2,
EXTRAS => 3
};
use constant INDEX => qw{HOUSE FAMILY EXTERN EXTRAS};
my @table;
while (<DATA>) {
push @table, [ split ];
}
print crosslink(HOUSE, FAMILY, 9), "\n";
print crosslink(HOUSE, FAMILY, 9), "\n";
print crosslink(FAMILY, HOUSE, 10), "\n";
print crosslink(EXTRAS, EXTERN, '8text'), "\n";
print crosslink(EXTRAS, FAMILY, '8text'), "\n";
print crosslink(HOUSE, EXTRAS, 12), "\n";
print crosslink(HOUSE, EXTRAS, 13), "\n";
{
my %cross;
sub crosslink {
my ($in, $out, $val) = @_;
my $key = $in . '-' . $val;
if (exists $cross{$key}) {
return $cross{$key}[$out];
}
print 'Search once only:', "\n"; # for testing only - remove
+ in production
my $found = first { $_->[$in] eq $val } @table;
return 'Not found! ' . (INDEX)[$in] . ": $val" if ! defined $f
+ound;
$cross{$key} = $found;
return $cross{$key}[$out];
}
}
__DATA__
1 2 3 4text
5 6 7 8text
9 10 11 12text
13 14 15 16text
When run, this produces identical output to that shown previously.
|