有没有一种Raku方法可以对哈希值进行排序并从其键值对中分离出来?

4

我目前正在尝试使用数组中的哈希值来查找每个特定项的键和值。在未对数组进行排序时,我可以做到这一点,并且键和值是分开的。但是当我创建排序后的数组时,例如:

my @sorted_pairs = %counts{$word}.sort(*.value);

它将值绑定在一起。是否有一种方法可以对排序后的哈希值进行处理,使得可以将键值对分开成数组中的单独实体?我想要能够单独访问“word”字符串和作为整数出现次数的计数。
我使用这个来源作为参考。我已经尝试了其中的几种方法,虽然按数字值对数组进行了排序,但输出结果显示,它们没有将键和值分开。
排序后的数组:[do => 1 rest => 1 look => 1 wanted => 1 give => 1 imagine => 2 read => 2 granted => 2 ever => 2 love => 2 gonna => 2 feel => 2 meant => 2 like => 2 you => 2 live => 3 wrote => 3 come => 3 know => 3 are => 3 mom => 4]

2
那么,%counts哈希表包含了什么内容呢?dd %counts{$word}会返回什么? - Elizabeth Mattijsen
2
有点不太明白你在做什么... 请提供一个自包含的 MRE。第一眼看起来,似乎可以使用 BagHash - librasteve
1
你是在讨论例如key1 => value1 key2 => value2中的value1 key2似乎彼此不分离吗?如果是这样,并且key1 => value1 key2 => value2背后的数据是哈希表或一组成对的列表,则表面上缺乏分离只是一个显示问题。数据是成对的,具有单独的键和值,我们可以轻松解释它。但这与排序无关。更普遍地说,您对问题的散文描述在很多方面都存在歧义。消除SO问题中歧义的标准方法是提供[MRE]。 - raiph
1
对于提供的“单词计数”答案,如果您想要访问@aoh数组的第一个元素,您可以写成:say @aoh[0];。要获取第一个元素的键,应该是:say @aoh[0].key;,要获取值,则应为:say @aoh[0].value;。范围也适用:.put for @aoh[0..3];会给您两列制表符分隔的键/值数据(@aoh数组的前4个“行”)。 - jubilatious1
1个回答

6

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#循环哈希键和值

不要使用 .reverse,而是按值的负数进行排序。 -*.value - Brad Gilbert
@BradGilbert 很有趣。我使用了.sort(*.values).reverse;,而你使用了.sort(-*.value);。顺便说一下,.sort(-*.values);是不起作用的,我无法解释这种差异(和/或缺乏错误消息)。 - jubilatious1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接