Raku中的字数统计
你可能想将结果保存为(Bag
)散列数组(对?),然后根据需要打印出.keys
或.values
。
raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
.say for @aoh;' Ishmael.txt
样例输入(Ishmael.txt
):
Call me Ishmael. Some years ago--never mind how long precisely --having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off--then, I account it high time to get to sea as soon as I can.
使用上述代码,您将获得以下输出结果(完整输出被截取,通过指定
$_.value >= 4
):
raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
.say if ($_.value >= 4) for @aoh ;' Ishmael.txt
I => 8
the => 7
and => 6
of => 4
to => 4
a => 4
如果将第二个语句更改为 .keys.put for @aoh
,就可以简单地返回只有 .keys
了。
$ raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
.keys.put if ($_.value >= 4) for @aoh ;' Ishmael.txt
I
the
and
a
to
of
或者,通过将第二个语句更改为
.values.put for @aoh
,仅返回
.values
:
$ raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
.values.put if ($_.value >= 4) for @aoh ;' Ishmael.txt
8
7
6
4
4
4
<注意:上面是在Raku中实现单词计数代码的一个相当快速且简略的例子。它不处理标点符号、大写等,但这是一个开始。>
https://docs.raku.org/language/hashmap#循环哈希键和值
dd %counts{$word}
会返回什么? - Elizabeth Mattijsenkey1 => value1 key2 => value2
中的value1 key2
似乎彼此不分离吗?如果是这样,并且key1 => value1 key2 => value2
背后的数据是哈希表或一组成对的列表,则表面上缺乏分离只是一个显示问题。数据是成对的,具有单独的键和值,我们可以轻松解释它。但这与排序无关。更普遍地说,您对问题的散文描述在很多方面都存在歧义。消除SO问题中歧义的标准方法是提供[MRE]。 - raiph@aoh
数组的第一个元素,您可以写成:say @aoh[0];
。要获取第一个元素的键,应该是:say @aoh[0].key;
,要获取值,则应为:say @aoh[0].value;
。范围也适用:.put for @aoh[0..3];
会给您两列制表符分隔的键/值数据(@aoh
数组的前4个“行”)。 - jubilatious1