Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: hash of unique words

by choroba (Canon)
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;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Comment on Re: hash of unique words
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2015-11-29 00:05 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (746 votes), past polls