Perl有序多维哈希(使用hash::Ordered)
我正试图在Perl中遍历多维哈希,顺序与我创建它的顺序完全相同。我正在使用Hash::Ordered,但无法使其工作 如果我使用Data::Dumper打印$MENU\u项,我会看到我期望的散列元素(见下文),但是,当我尝试将它们吸收到$ITEM中时,它是空的 我肯定我在做傻事,请帮忙:)Perl有序多维哈希(使用hash::Ordered),perl,hash,Perl,Hash,我正试图在Perl中遍历多维哈希,顺序与我创建它的顺序完全相同。我正在使用Hash::Ordered,但无法使其工作 如果我使用Data::Dumper打印$MENU\u项,我会看到我期望的散列元素(见下文),但是,当我尝试将它们吸收到$ITEM中时,它是空的 我肯定我在做傻事,请帮忙:) 感谢@Dada的帮助,以下是工作代码: use Hash::Ordered; $MENU_SECTIONS = Hash::Ordered->new ( 'MENU_manage_pro
感谢@Dada的帮助,以下是工作代码:
use Hash::Ordered;
$MENU_SECTIONS = Hash::Ordered->new
(
'MENU_manage_properties' =>
Hash::Ordered->new(
'show_current_properties' => 'Current Properties',
'show_prospect_properties' => 'Prospect Properties'
),
'MENU_reports' =>
Hash::Ordered->new(
'some_report' => 'Some report',
'another_report' => 'Another report'
),
'MENU_settings' =>
Hash::Ordered->new(
'settings_here' => 'Settings',
'more_settings' => 'More Settings'
)
);
$menu_iterator = $MENU_SECTIONS->iterator;
while( (my $MENU_SECTION, my $MENU_ITEMS) = $menu_iterator->() )
{
my $ITEM = Hash::Ordered->new($MENU_ITEMS->as_list);
$item_iterator = $ITEM->iterator;
while( (my $OPERATION, my $TITLE) = $item_iterator->() )
{
## DO something with $OPERATION and $TITLE
}
}
始终
严格使用;使用警告
。总是。您编写了@MENU_ITEMS
,而不是@$MENU_ITEMS
strict
会警告您。对,$MENU\u ITEMS
不是数组引用,而是hashref。我不知道你想用它做什么,但你应该知道。在任何情况下,@MENU\u ITEMS
都不是一个存在的变量,因此您应该看到代码有问题。可能是%$MENU\u ITEMS
(尽管将无序散列转换为有序散列可能没有多大意义),这是因为您使用{…}创建了散列
而您应该在第一个哈希::有序->新建中使用哈希::有序->新建。我想知道您是否不应该使用数组而不是哈希。
my MENU_manage_properties, my HASH(0x43a21f0)
Dumper print of MENU_ITEMS:
$VAR1 = { 'show_current_properties' => 'Current Properties', 'show_prospect_properties' => 'Prospect Properties' };
Dumper print of ITEM:
$VAR1 = bless( [ {}, [], undef, 0, 0 ], 'Hash::Ordered' );
Dumper print of item_iterator:
$VAR1 = sub { "DUMMY" };
my MENU_tenants, my HASH(0x4397b80)
Dumper print of MENU_ITEMS:
$VAR1 = { 'show_current_tenants' => 'Current Tenants', 'show_prospect_tenants' => 'Prospect Tenants' };
Dumper print of ITEM:
$VAR1 = bless( [ {}, [], undef, 0, 0 ], 'Hash::Ordered' );
Dumper print of item_iterator:
$VAR1 = sub { "DUMMY" };
my MENU_buyers, my HASH(0x4317590)
Dumper print of MENU_ITEMS:
$VAR1 = { 'show_prospect_buyers' => 'Prospect Buyers', 'show_current_buyers' => 'Current Buyers' };
Dumper print of ITEM:
$VAR1 = bless( [ {}, [], undef, 0, 0 ], 'Hash::Ordered' );
Dumper print of item_iterator:
$VAR1 = sub { "DUMMY" };
my MENU_reports, my HASH(0x43175f0)
Dumper print of MENU_ITEMS:
$VAR1 = { 'some_report' => 'Some report', 'another_report' => 'Another report' };
Dumper print of ITEM:
$VAR1 = bless( [ {}, [], undef, 0, 0 ], 'Hash::Ordered' );
Dumper print of item_iterator:
$VAR1 = sub { "DUMMY" };
my MENU_settings, my HASH(0x4317650)
Dumper print of MENU_ITEMS:
$VAR1 = { 'more_settings' => 'More Settings', 'settings_here' => 'Settings' };
Dumper print of ITEM:
$VAR1 = bless( [ {}, [], undef, 0, 0 ], 'Hash::Ordered' );
Dumper print of item_iterator:
$VAR1 = sub { "DUMMY" };
use Hash::Ordered;
$MENU_SECTIONS = Hash::Ordered->new
(
'MENU_manage_properties' =>
Hash::Ordered->new(
'show_current_properties' => 'Current Properties',
'show_prospect_properties' => 'Prospect Properties'
),
'MENU_reports' =>
Hash::Ordered->new(
'some_report' => 'Some report',
'another_report' => 'Another report'
),
'MENU_settings' =>
Hash::Ordered->new(
'settings_here' => 'Settings',
'more_settings' => 'More Settings'
)
);
$menu_iterator = $MENU_SECTIONS->iterator;
while( (my $MENU_SECTION, my $MENU_ITEMS) = $menu_iterator->() )
{
my $ITEM = Hash::Ordered->new($MENU_ITEMS->as_list);
$item_iterator = $ITEM->iterator;
while( (my $OPERATION, my $TITLE) = $item_iterator->() )
{
## DO something with $OPERATION and $TITLE
}
}