hashtable - lookup in a multidimensional hash perl -
hashtable - lookup in a multidimensional hash perl -
i have info construction (a multidimensional hash table):
$var1 = { 'cat' => { "félin" => '0.500000', 'chat' => '0.600000' }, 'rabbit' => { 'lapin' => '0.600000' }, 'canteen' => { "ménagère" => '0.400000', 'cantine' => '0.600000' } };
my goal read tokenized text, , each word need find translation(s). tokens, read text , create array that:
##store each word translate in table while(my $text_to_translate = <texttotranslate>) { @temp = split(/ /, $text_to_translate); push(@tokens, @temp); }
my problem find best way (and fastest) lookup bidimensionnal hash table , print possible translation(s) that:
i love cat[chat;félin] , rabbit[lapin].
for now, had create loop did'nt work , think not best way:
foreach $source (sort keys %hash) { foreach $target (keys %{ $hash{$source} }) { $val = $source; ##loop each tokens foreach (@tokens) { $actualword = $_; if($val eq $actualword){ print $actualword."==>".$target."\n"; } else{ print $actualword."\n"; next; } } } }
thank help.
what :
#!/usr/bin/env perl utilize strict; utilize warnings; $hash = { 'cat' => { "félin" => '0.500000', 'chat' => '0.600000' }, 'rabbit' => { 'lapin' => '0.600000' }, 'canteen' => { "ménagère" => '0.400000', 'cantine' => '0.600000' } }; $text = "i love cat , rabbit canteen !\n"; foreach $word (split /\s/, $text) { print $word; exists $hash->{$word} , print "[" . join(";", keys %{ $hash->{$word} }) . "]"; print " "; } print "\n";
output:
i love cat[chat;félin] , rabbit[lapin] canteen[cantine;ménagère] !
multidimensional-array hashtable lookup perl-data-structures
Comments
Post a Comment