in reply to One to one relationship

The replies provided were fantastic.. I am not really sure if a bidirectional hash with key-value value-key combinations can also be considered and equally efficient in case you have a large amount of relations. Dear Monks please be free to criticize this, I aspire to learn by interaction.

This code here can let you treat all the right and left values as keys and as values of each other, then you may want to use the hash as a search dictionary:

#!/usr/local/bin/perl #title "One to one relationship"; use strict; use warnings; use diagnostics; my %hash; while(<DATA>){ next unless /"(\w+)".*"(\w+)"/gs; $hash{$1}=$2; $hash{$2}=$1; } use Data::Dumper; print Data::Dumper->Dump ([\%hash]); __DATA__ "ger" <--> "german" "ara" <--> "arabic" "eng" <--> "english"

here's the output:

$VAR1 = { 'arabic' => 'ara', 'ara' => 'arabic', 'ger' => 'german', 'german' => 'ger', 'english' => 'eng', 'eng' => 'english' };


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^2: One to one relationship
by ack (Deacon) on Oct 30, 2009 at 16:16 UTC

    This strategy is exactly what I was thinking, too. It saves having to deal with two different hashes (which several of the other responders recommended and certainly would work as they offer).

    As ikegami notes, however, either this strategy (where only a single hash is used) or the two-hash strategy, only works if it is a true one-to-one mapping of keys to values. If that requirement is met, then it seems to me that neither strategy will work. In that case, I think the op has a whole different challenge.

    ack Albuquerque, NM