perl中多维数组的哈希填充

发布于 2024-10-01 02:21:49 字数 1045 浏览 0 评论 0原文

给定三个标量,填充散列的 Perl 语法是什么,其中一个标量是键,另一个标量确定填充两个数组中的哪一个,第三个标量附加到其中一个数组?例如:

my $weekday = "Monday";
my $kind    = "Good";
my $event   = "Birthday";

并且只给出在循环内获得的标量而不是它们的特定值,我想要一个像这样的散列:

my %Weekdays = {
      'Monday' => [
                    ["Birthday", "Holiday"],     # The Good array
                    ["Exam", "Workday"]          # The Bad array
                  ]
      'Saturday' => [
                    ["RoadTrip", "Concert", "Movie"],
                    ["Yardwork", "VisitMIL"]
                  ]
}

我知道如何将值附加到散列中的数组,例如如果键是单个数组:

push( @{ $Weekdays{$weekday} }, $event);

使用在循环中,这可能会给我:

%Weekdays = {
        'Monday' => [
                    'Birthday',
                    'Holiday',
                    'Exam',
                    'Workday'
                    ]
}

我认为哈希键是特定的工作日,并且该值应该是二维数组。我不知道 perl 语法,例如,将生日作为工作日数组的元素 [0][0] 推入散列,并在下一次循环中将另一个事件推入为 [0][1] 或[1][0]。同样,我不知道访问相同的语法。

Given three scalars, what is the perl syntax to fill a hash in which one of the scalars is the key, another determines which of two arrays is filled, and the third is appended to one of the arrays? For example:

my $weekday = "Monday";
my $kind    = "Good";
my $event   = "Birthday";

and given only the scalars and not their particular values, obtained inside a loop, I want a hash like:

my %Weekdays = {
      'Monday' => [
                    ["Birthday", "Holiday"],     # The Good array
                    ["Exam", "Workday"]          # The Bad array
                  ]
      'Saturday' => [
                    ["RoadTrip", "Concert", "Movie"],
                    ["Yardwork", "VisitMIL"]
                  ]
}

I know how to append a value to an array in a hash, such as if the key is a single array:

push( @{ $Weekdays{$weekday} }, $event);

Used in a loop, that could give me:

%Weekdays = {
        'Monday' => [
                    'Birthday',
                    'Holiday',
                    'Exam',
                    'Workday'
                    ]
}

I suppose the hash key is the particular weekday, and the value should be a two dimensional array. I don't know the perl syntax to, say, push Birthday into the hash as element [0][0] of the weekday array, and the next time through the loop, push another event in as [0][1] or [1][0]. Similarly, I don't know the syntax to access same.

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

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

发布评论

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

评论(2

无法言说的痛 2024-10-08 02:21:49

使用您的变量,我会这样写:

push @{ $Weekdays{ $weekday }[ $kind eq 'Good' ? 0 : 1 ] }, $event;

但是,我可能也会制作好/坏说明符。并考虑到我的建议:

use autobox::Core; 
( $Weekdays{ $weekday }{ $kind } ||= [] )->push( $event );

请注意,我在这里编写的方式,在我们开始之前,两个表达式都不关心数组是否存在。

Using your variables, I'd write it like this:

push @{ $Weekdays{ $weekday }[ $kind eq 'Good' ? 0 : 1 ] }, $event;

However, I'd probably just make the Good/Bad specifiers keys as well. And given my druthers:

use autobox::Core; 
( $Weekdays{ $weekday }{ $kind } ||= [] )->push( $event );

Note that the way I've written it here, neither expression cares whether or not an array exists before we start.

丢了幸福的猪 2024-10-08 02:21:49

有什么原因

 push @{ $Weekdays{Monday}[0] }, "whatever";

对你不起作用吗?

Is there some reason that

 push @{ $Weekdays{Monday}[0] }, "whatever";

isn’t working for you?

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