是否有 Raku 方法允许对哈希值进行排序并从其对中拆分?

Hom*_*an 5 hash rakudo raku

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

my @sorted_pairs = %counts{$word}.sort(*.value);
Run Code Online (Sandbox Code Playgroud)

它将值绑定在一起。是否有一种排序哈希值的方法,允许将哈希值对拆分为数组中的单独实体?我希望能够分别访问“单词”字符串以及该单词被视为整数的计数或次数。

我使用这个来源作为参考。我已经尝试了其中一些方法,虽然它似乎确实按给定输出的数值对数组进行排序:

排序数组:[做=> 1休息=> 1看=> 1想要=> 1给予=> 1想象=> 2阅读=> 2授予=> 2曾经=> 2爱=> 2将=> 2感觉= > 2意味着=> 2喜欢=> 2你=> 2生活=> 3写=> 3来=> 3知道=> 3是=> 3妈妈=> 4]

它不会将键和值彼此分开。

jub*_*us1 6

Raku 字数统计

您可能希望将结果保存为(Bag-ged)哈希数组(成对?),然后根据需要打印出.keys或。.values

raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
        .say for @aoh;'  Ishmael.txt
Run Code Online (Sandbox Code Playgroud)

输入示例 ( 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.
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,您将得到以下输出(通过指定 截断完整输出$_.value >= 4):

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.
Run Code Online (Sandbox Code Playgroud)

只需.keys将第二条语句更改为即可返回,非常简单.keys.put for @aoh

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
Run Code Online (Sandbox Code Playgroud)
$ raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
          .keys.put if ($_.value >= 4) for @aoh ;'  Ishmael.txt
Run Code Online (Sandbox Code Playgroud)

.values或者只需将第二个语句更改为即可返回.values.put for @aoh

I
the
and
a
to
of
Run Code Online (Sandbox Code Playgroud)
$ raku -e 'my @aoh = words.Bag.pairs.sort(*.values).reverse; \
          .values.put if ($_.value >= 4) for @aoh ;'  Ishmael.txt
Run Code Online (Sandbox Code Playgroud)

[注意:上面是 Raku 中字数统计代码的一个非常快速且简单的示例。它不处理标点符号、大写等,但它是一个开始。]

https://docs.raku.org/language/hashmap#Looping_over_hash_keys_and_values