是否有Raku方法可以将哈希值与其对分配并拆分?

发布于 2025-01-25 04:44:49 字数 707 浏览 2 评论 0原文

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

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

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

我正在使用此 source 作为参考。我尝试了一些这些方法,虽然它似乎确实按数字值对数字进行排序,但给定输出:

排序的数组:[do => 1 rest => 1 look => 1通缉=> 1 give => 1 想象=> 2读=> 2授予=> 2 ever => 2爱=> 2 gong => 2 感觉=> 2表示=> 2喜欢=> 2 you => 2 live => 3写作=> 3 come => 3 知道=> 3是=> 3妈妈=> 4]

它不会将密钥和价值与彼此分开。

I am currently trying to use hashes in an array to find the keys and values of each specific item in the array. I am able to do this and both the keys and the values are separate when I haven't sorted the array, but when I create a sorted array such as:

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

It binds the values together. Is there a method for sorted hash values that allow the pairs to be split into separate entities within the array? I want to be able to access the "word" string and the count or number of times that word was seen as an integer, separately.

I am using this source as a reference. I have tried a handful of these methods and while it does seem to sort the array by numeric value given the output:

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]

it doesn't separate the key and value from one another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2025-02-01 04:44:49

raku中的单词计数

您可能需要将结果保存为(bag - ged) - 哈什阵列(Pairs?),然后打印出一个

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 @AOH

$ 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.values.put @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中单词计数代码的示例。它无法处理标点符号,大写等,但它是一个开始

。 https://docs.raku.org/language/hashmap#looping_over_hash_keys_and_values

Word-counting in Raku

You might want to save your results as a (Bag-ged) array-of-hashes (pairs?), then print out either .keys or .values as necessary.

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

Sample Input (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.

Using the code above you get the following Output (full output truncated by specifying $_.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

And it's simple enough to return just .keys by changing the second statement to .keys.put for @aoh:

$ 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

Or return just .values by changing the second statement to .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

[Note: the above is a pretty quick-and-dirty example of word-counting code in Raku. It doesn't handle punctuation, capitalization, etc., but it's a start.]

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文