perl中多维数组的哈希填充
给定三个标量,填充散列的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用您的变量,我会这样写:
但是,我可能也会制作好/坏说明符键。并考虑到我的建议:
请注意,我在这里编写的方式,在我们开始之前,两个表达式都不关心数组是否存在。
Using your variables, I'd write it like this:
However, I'd probably just make the Good/Bad specifiers keys as well. And given my druthers:
Note that the way I've written it here, neither expression cares whether or not an array exists before we start.
有什么原因
对你不起作用吗?
Is there some reason that
isn’t working for you?