是否有一种 Raku 方法允许对散列值进行排序并从其对中拆分出来?
Is there a Raku method that allows a hash value to be sorted and split from its pair?
我目前正尝试在数组中使用散列来查找数组中每个特定项目的键和值。我能够做到这一点,当我没有对数组进行排序时,键和值都是分开的,但是当我创建一个排序数组时,例如:
my @sorted_pairs = %counts{$word}.sort(*.value);
它将值绑定在一起。是否有一种方法可以将散列值对拆分为数组中的单独实体?我希望能够分别访问“单词”字符串和该单词被视为整数的次数或次数。
我正在使用此 作为参考。我已经尝试了一些这些方法,虽然它似乎确实在给定输出的情况下按数值对数组进行排序:
sorted array : [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]
它不会将键和值彼此分开。
Word-counting 在 Raku
您可能希望将结果保存为 (Bag
-ged) array-of-hashes(成对?),然后根据需要打印出 .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
return 就足够简单了
$ 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
或 return 只是 .values
通过将第二个语句更改为 .values.put for @aoh
:
$ 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 中 word-counting 代码的漂亮 quick-and-dirty 示例。它不处理标点符号、大小写等,但这是一个开始。]
https://docs.raku.org/language/hashmap#Looping_over_hash_keys_and_values
我目前正尝试在数组中使用散列来查找数组中每个特定项目的键和值。我能够做到这一点,当我没有对数组进行排序时,键和值都是分开的,但是当我创建一个排序数组时,例如:
my @sorted_pairs = %counts{$word}.sort(*.value);
它将值绑定在一起。是否有一种方法可以将散列值对拆分为数组中的单独实体?我希望能够分别访问“单词”字符串和该单词被视为整数的次数或次数。
我正在使用此
sorted array : [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]
它不会将键和值彼此分开。
Word-counting 在 Raku
您可能希望将结果保存为 (Bag
-ged) array-of-hashes(成对?),然后根据需要打印出 .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
return 就足够简单了
$ 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
或 return 只是 .values
通过将第二个语句更改为 .values.put for @aoh
:
$ 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 中 word-counting 代码的漂亮 quick-and-dirty 示例。它不处理标点符号、大小写等,但这是一个开始。]
https://docs.raku.org/language/hashmap#Looping_over_hash_keys_and_values