如何在三个值之间"映射"?

San*_*ing 5 perl data-structures

我有很多像这样的数据

type1, type2, type3
aax, ert, ddd
asx, eer, kkk
xkk, fff, lll
xxj, vtt, lle
...
Run Code Online (Sandbox Code Playgroud)

我真的希望能够在他们之间"映射",所以我可以去

type1 -> type2
type1 -> type3
type2 -> type1
type3 -> type1
Run Code Online (Sandbox Code Playgroud)

例:

type1_to_type2(aax) should return ert
type1_to_type3(asx) should return kkk
type2_to_type3(fff) should return lll
type3_to_type1(lle) should return xxj
Run Code Online (Sandbox Code Playgroud)

应该使用什么数据结构的数据?

这些功能怎么样?

更新:所有数据都是唯一的.

Dav*_*oss 3

实际实现“type1_to_type2”等功能的版本。

#!/usr/bin/perl

use strict;
use warnings;

my $data;
while (<DATA>) {
  chomp;
  push @$data, [ split ];
}

sub typeX_to_typeY {
  my ($x, $y, $str) = @_;

  foreach (@$data) {
    if ($_->[$x - 1] eq $str) {
      return $_->[$y - 1];
    }
  }

  return;
}

sub type1_to_type2 { typeX_to_typeY(1, 2, @_) }
sub type1_to_type3 { typeX_to_typeY(1, 3, @_) }
sub type2_to_type1 { typeX_to_typeY(2, 1, @_) }
sub type2_to_type3 { typeX_to_typeY(2, 3, @_) }
sub type3_to_type1 { typeX_to_typeY(3, 1, @_) }
sub type3_to_type2 { typeX_to_typeY(3, 2, @_) }

# tests
use Test::More tests => 4;

is(type1_to_type2('aax'), 'ert');
is(type1_to_type3('asx'), 'kkk');
is(type2_to_type3('fff'), 'lll');
is(type3_to_type1('lle'), 'xxj');

__DATA__
aax ert ddd
asx eer kkk
xkk fff lll
xxj vtt lle
Run Code Online (Sandbox Code Playgroud)