my $words = { 'lng' => ['lang', 'long', 'leng', 'loo'], 'sentance' => ['sentence'], 'thxs' => ['this', 'thus'] }; print map "$_\n", phol($words); sub phol { my($key, $value, @rest) = %{+shift}; if (@rest) { return map { my $r = $_; map { "$_ $r" } @$value } phol({@rest}); } else { return @$value; } }