我有哈希的哈希,我正在尝试用每个哈希值填充一个选择框。 无论如何,我无法访问我的内部哈希变量。 我能够在我的选择中生成正确数量的选项,但我目前只能设置每个选择选项的值参数。
继承人我的哈希:
my $export_types = { a => {label => "Foo", ext => ".js"}, b => {label => "Bar", ext => ".gz"}};这是迄今为止我为我的foreach尝试过的事情:
my $select = "<select id='fancy'>"; foreach my $key (sort keys %{$export_types}) { $select .= "<option value='$key' "; if($saved_value eq $key || (!$saved_value && $key eq "a")) { $select .="selected='selected'"; } $select .= ">".$export_types{$key}{label}."</option>"; } $select .= "</select>";显然我正在访问标签属性错误。 对于那条特定的线路,我也尝试过:
$select .= ">".$export_types{$key}->{label}."</option>";但那也无济于事。 我确定我错过了一些简单的事情。
谢谢您的帮助 :)
I've got this hash of hashes and I'm trying to populate a select box with values from each hash. Anyway I'm having trouble getting to my inner hash variables. I am able to generate the right number of options in my select, but I'm currently only able to set the value parameter of each select option.
Heres my hash:
my $export_types = { a => {label => "Foo", ext => ".js"}, b => {label => "Bar", ext => ".gz"}};Heres what I've tried so far for my foreach:
my $select = "<select id='fancy'>"; foreach my $key (sort keys %{$export_types}) { $select .= "<option value='$key' "; if($saved_value eq $key || (!$saved_value && $key eq "a")) { $select .="selected='selected'"; } $select .= ">".$export_types{$key}{label}."</option>"; } $select .= "</select>";apparently I'm accessing the label property wrong. For that particular line I also tried:
$select .= ">".$export_types{$key}->{label}."</option>";but that was to no avail as well. I'm sure I'm missing something simple.
Thanks for the help :)
最满意答案
表达方式
$export_types{$key}{label}假设存在散列%export_types 。 不是这种情况。 如果你有一个use strict的use strict范围,你会被提醒这个事实。
因为$export_types是一个哈希引用 ,所以我们必须在使用下标运算符访问某个值之前取消引用它。 或
$export_types->{$key}{label}要么
$$export_types{$key}{label}(我更喜欢前者)。
The expression
$export_types{$key}{label}assumes that there is a hash %export_types. This is not the case. If you had a use strict in scope, you would have been alerted to this fact.
Because $export_types is a hash reference, we have to dereference it before using the subscript operator to access some value. Either
$export_types->{$key}{label}or
$$export_types{$key}{label}(I prefer the former).
更多推荐
发布评论