colicab has asked for the wisdom of the Perl Monks concerning the following question:

Hi there.

I have a fairly basic question. I hope this is the right place for it.

I have a 4 column table: the first column is the inhouse ID of an individidual, the second is the family ID, the 3rd is an external ID and the last column give me some additional information about that person.

I'm looking for a way (an existing module, object type...) to access any column I want given any ID I give in. For example, I have the inhouse person ID, and I want the family ID (for simplicity suppose all these IDs occur only one in the entire table). For the moment I tackled this issue by making a complex hash structure, bit it looks a bit convoluted and I'm looking for a more elegant, less error-prone way to code this. I was imagining an object type where you have methods like ''inhouse2family" so I can get what I want by typing:

 my $family_ID = $object->inhouse2family(<inhouse ID comes here>)

I was just attempting to make an object (it would have been my first object I write) that does something like this, but it felt like a 'basic' feature that maybe someone else already found an elegant solution for. Does anyone know one?

Thanks!
  • Comment on Looking for an existing package to crosslink different IDs with each other
  • Download Code

Replies are listed 'Best First'.
Re: Looking for an existing package to crosslink different IDs with each other
by GrandFather (Saint) on Oct 01, 2012 at 00:24 UTC

    Are you using a database or some form of in-memory data storage? If you are using a database there shouldn't be a problem. If you are storing the data in memory you could still use a database (SQLite allows in-memory storage - use ":memory:" for the file name). If, despite the terms you have used, you aren't database savvy, use a hash keyed by the inhouse ID as the table with a hash per row containing the data, then use a lookup hash per other indexed column which gives the inhouse ID as the value. Wrapping the various hashes up in an object makes good sense.

    True laziness is hard work
Re: Looking for an existing package to crosslink different IDs with each other
by kcott (Archbishop) on Oct 01, 2012 at 00:12 UTC

    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

      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