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.

-- Ken


In reply to Re^3: Looking for an existing package to crosslink different IDs with each other by kcott
in thread Looking for an existing package to crosslink different IDs with each other by colicab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.