Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: hash of unique words

by choroba (Bishop)
on Apr 22, 2013 at 16:37 UTC ( #1029922=note: print w/replies, xml ) Need Help??

in reply to hash of unique words

Rather than hash of arrays, use a hash of hashes. At the end, replace each inner hash with its number of keys:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; open my $ENGLISH, '<', 'corpus.e' or die $!; open my $FOREIGN, '<', 'corpus.f' or die $!; chomp(my @sents_en = <$ENGLISH>); chomp(my @sents_f = <$FOREIGN>); my %uniform; for my $sentence_index (0 .. $#sents_en) { my @words_en = split ' ', $sents_en[$sentence_index]; my @words_f = split ' ', $sents_f[$sentence_index]; for my $word_index (0 .. $#words_en) { $uniform{ $words_en[$word_index] }{$_}++ for @words_f; } } for my $word (keys %uniform) { $uniform{$word} = keys %{ $uniform{$word} }; } print Dumper \%uniform;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: hash of unique words
by Ninke (Novice) on Apr 22, 2013 at 17:50 UTC
    Choroba, thanx very much, that does exactly what I want. A nice trick with {$_} for @words, that reduced the number of lines twice:) Though I don't understand the magic to the end, especially when a hash of hashes ($uniform{ $words_en$word_index }{$_}) turnes into a one-dimentional hash: $uniform{$word}. I just need to use it in practice and then I'll get it:)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029922]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-12-16 00:36 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (447 votes). Check out past polls.