从一个简单的数组中递归构建嵌套哈希

发布于 2025-02-09 07:06:36 字数 837 浏览 1 评论 0原文

得到了这个:

my @list = <one two three>;

my %hash;
my $item1 = @list.shift;
%hash{$item1} = {$item1 => 1};

my $item2 = @list.shift;
%hash{$item1} = {$item2 => 1};

my $item3 = @list.shift;
%hash{$item1}{$item2} = {$item3 => 1};

say %hash;

输出此期望的数据结构:

{one => {two => {three => 1}}}

显然,如果递归是递归的话,这会更好,所以我写了此:

sub get-hash(%parent-hash, $last-item, *@list) {
    my $item = @list.shift;
    %parent-hash{$last-item} = { $item => 1 };
    get-hash(%parent-hash{$last-item}, $item, @list) if @list;

    return %parent-hash<root>;
}

%hash = get-hash({}, 'root', @list2);

输出:

{one => {two => {three => 1}}}

虽然有效,但它感觉很不高,尤其是必须传递root参数到潜艇,然后将其删除。有什么建议吗?

Got this:

my @list = <one two three>;

my %hash;
my $item1 = @list.shift;
%hash{$item1} = {$item1 => 1};

my $item2 = @list.shift;
%hash{$item1} = {$item2 => 1};

my $item3 = @list.shift;
%hash{$item1}{$item2} = {$item3 => 1};

say %hash;

Outputs this desired data structure:

{one => {two => {three => 1}}}

Obviously, this would be better if it were recursive, so I wrote this:

sub get-hash(%parent-hash, $last-item, *@list) {
    my $item = @list.shift;
    %parent-hash{$last-item} = { $item => 1 };
    get-hash(%parent-hash{$last-item}, $item, @list) if @list;

    return %parent-hash<root>;
}

%hash = get-hash({}, 'root', @list2);

Output:

{one => {two => {three => 1}}}

Though it works, it feels inelegant, especially having to pass in a root argument to the sub and then removing it. Any suggestions?

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

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

发布评论

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

评论(3

樱娆 2025-02-16 07:06:36

在即将推出的Raku版本中,有一种整洁的方法:

use v6.e.PREVIEW;
my @list = <one two three>;
my %hash;
%hash{||@list} = 1;
say %hash;

||表示您想将列表使用为多维 hash键。

如果您想在当前发布的语言版本中坚持使用内容,则仍然可以直接致电操作员,因为只有语法糖丢失了:

my @list = <one two three>;
my %hash;
postcircumfix:<{; }>(%hash, @list) = 1;
say %hash

两种情况下的输出都是您想要的:

{one => {two => {three => 1}}}

In the upcoming Raku version, there's a neat way to do this:

use v6.e.PREVIEW;
my @list = <one two three>;
my %hash;
%hash{||@list} = 1;
say %hash;

The || indicates that you want to use the list as multi-dimensional hash keys.

If you want to stick to things in the current released language versions, you can still call the operator directly, since it's only the syntax sugar that is missing:

my @list = <one two three>;
my %hash;
postcircumfix:<{; }>(%hash, @list) = 1;
say %hash

The output in either case is as you wish:

{one => {two => {three => 1}}}
我不在是我 2025-02-16 07:06:36

好的,玩参数的顺序有助于简化事情:

sub get-hash(@list, %parent-hash = {}, $last-item = 'root') {
    my $item = @list.shift;
    %parent-hash{$last-item} = { $item => 1 };
    get-hash(@list, %parent-hash{$last-item}, $item) if @list;

    return %parent-hash<root>;
}

my @list2 = <one two three>;
%hash = get-hash(@list2);

OK, playing around with the order of the arguments helped simplify things a bit:

sub get-hash(@list, %parent-hash = {}, $last-item = 'root') {
    my $item = @list.shift;
    %parent-hash{$last-item} = { $item => 1 };
    get-hash(@list, %parent-hash{$last-item}, $item) if @list;

    return %parent-hash<root>;
}

my @list2 = <one two three>;
%hash = get-hash(@list2);

甚是思念 2025-02-16 07:06:36

想要“键值”结构

my @list = <one two three>;
say [=>] |@list, 1

<one two three>
andthen |$_, 1
andthen .reduce: sub ($x,$y) is assoc<right> { %( $x => $y ) }\
andthen .say

如果

<one two three>
andthen 1, |.reverse
andthen .reduce:  { %( $^y => $^x ) }\
andthen .say

If you want ’key-value’ structure Pair

my @list = <one two three>;
say [=>] |@list, 1

If you really need Hash

<one two three>
andthen |$_, 1
andthen .reduce: sub ($x,$y) is assoc<right> { %( $x => $y ) }\
andthen .say

or

<one two three>
andthen 1, |.reverse
andthen .reduce:  { %( $^y => $^x ) }\
andthen .say
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文