Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Looking for an existing package to crosslink different IDs with each other

by kcott (Abbot)
on Oct 01, 2012 at 00:12 UTC ( #996553=note: print w/ replies, xml ) Need Help??


in reply to Looking for an existing package to crosslink different IDs with each other

G'day colicab,

I'm not aware of a module that does this. Here's a fairly simple subroutine that may do what you want.

#!/usr/bin/env perl use 5.010; 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; my %cross; while (<DATA>) { push @table, [ split ]; } say crosslink(HOUSE, FAMILY, 9); say crosslink(HOUSE, FAMILY, 9); say crosslink(FAMILY, HOUSE, 10); say crosslink(EXTRAS, EXTERN, '8text'); say crosslink(EXTRAS, FAMILY, '8text'); say crosslink(HOUSE, EXTRAS, 12); say crosslink(HOUSE, EXTRAS, 13); sub crosslink { my ($in, $out, $val) = @_; my $key = $in . '-' . $val; if (exists $cross{$key}) { return $cross{$key}[$out]; } say 'Search once only:'; # for testing only - remove in producti +on my $found = first { $_->[$in] eq $val } @table; return 'Not found! ' . (INDEX)[$in] . ": $val" if ! defined $found +; $cross{$key} = $found; return $cross{$key}[$out]; } __DATA__ 1 2 3 4text 5 6 7 8text 9 10 11 12text 13 14 15 16text

Output:

$ pm_cross_table.pl Search once only: 10 10 Search once only: 9 Search once only: 7 6 Search once only: Not found! HOUSE: 12 Search once only: 16text

-- Ken


Comment on Re: Looking for an existing package to crosslink different IDs with each other
Select or Download Code
Re^2: Looking for an existing package to crosslink different IDs with each other
by colicab (Initiate) on Oct 01, 2012 at 14:13 UTC

    Thanks a lot kcott!

    This is exactly what I need!!

    One question: to run this script, is version 5.10 required? I just checked the perl version on our server and it's outdated (version 5.8.8). Can I do it with this one? Else I'll have to go to the system admin to ask to update this.

    Thanks again!
      As far as I can see, the only 5.10 feature used in the code is say. In older Perls, use print with a "\n" at the end, or define
      sub say { print @_, "\n"; }
      .
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996553]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2014-10-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls