my @wordsList; for my $word ( keys %{ $hash{ $w1 }{ $w2 }{ words } } ) { push @wordsList, ( $key ) x $hash{ $w1 }{ $w2 }{ words }{ $key }; } print @wordsList[ rand( @wordsList ) ];