Perl - 有序多维哈希(使用 Hash::Ordered)
Perl - Ordered multi-dimensional hash (use Hash::Ordered)
我正在尝试按照我创建它的确切顺序在 Perl 中遍历多维哈希。我正在使用 Hash::Ordered,但无法正常工作。
如果我使用 Data::Dumper 打印 $MENU_ITEMS 我会看到我期望的散列元素(见下文),但是,当我尝试将它们引入 $ITEM 时,它是空白的。
我确定我在做一些愚蠢的事情,请帮忙:)
use Hash::Ordered;
$MENU_SECTIONS = Hash::Ordered->new
(
'MENU_manage_properties' =>
{
'show_current_properties' => 'Current Properties',
'show_prospect_properties' => 'Prospect Properties'
},
'MENU_reports' =>
{
'some_report' => 'Some report',
'another_report' => 'Another report'
},
'MENU_settings' =>
{
'settings_here' => 'Settings',
'more_settings' => 'More Settings'
}
);
%MENU_SECTIONS_HTML = ();
use Data::Dumper;
print header();
$menu_iterator = $MENU_SECTIONS->iterator;
while( (my $MENU_SECTION, my $MENU_ITEMS) = $menu_iterator->() ) 5
{
print "<hr>my $MENU_SECTION, my $MENU_ITEMS<br>";
print "Dumper print of MENU_ITEMS:<br>";
print Dumper $MENU_ITEMS;
print "<br><br>";
my $ITEM = Hash::Ordered->new(@MENU_ITEMS);
$item_iterator = $ITEM->iterator;
print "Dumper print of ITEM:<br>";
print Dumper $ITEM;
print "<br><br>";
print "Dumper print of item_iterator:<br>";
print Dumper $item_iterator;
print "<br><br>";
while( (my $OPERATION, my $TITLE) = $item_iterator->() )
{
print "my $OPERATION, my $TITLE<be>";
}
}
我得到的输出:
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
}
}
我正在尝试按照我创建它的确切顺序在 Perl 中遍历多维哈希。我正在使用 Hash::Ordered,但无法正常工作。
如果我使用 Data::Dumper 打印 $MENU_ITEMS 我会看到我期望的散列元素(见下文),但是,当我尝试将它们引入 $ITEM 时,它是空白的。
我确定我在做一些愚蠢的事情,请帮忙:)
use Hash::Ordered;
$MENU_SECTIONS = Hash::Ordered->new
(
'MENU_manage_properties' =>
{
'show_current_properties' => 'Current Properties',
'show_prospect_properties' => 'Prospect Properties'
},
'MENU_reports' =>
{
'some_report' => 'Some report',
'another_report' => 'Another report'
},
'MENU_settings' =>
{
'settings_here' => 'Settings',
'more_settings' => 'More Settings'
}
);
%MENU_SECTIONS_HTML = ();
use Data::Dumper;
print header();
$menu_iterator = $MENU_SECTIONS->iterator;
while( (my $MENU_SECTION, my $MENU_ITEMS) = $menu_iterator->() ) 5
{
print "<hr>my $MENU_SECTION, my $MENU_ITEMS<br>";
print "Dumper print of MENU_ITEMS:<br>";
print Dumper $MENU_ITEMS;
print "<br><br>";
my $ITEM = Hash::Ordered->new(@MENU_ITEMS);
$item_iterator = $ITEM->iterator;
print "Dumper print of ITEM:<br>";
print Dumper $ITEM;
print "<br><br>";
print "Dumper print of item_iterator:<br>";
print Dumper $item_iterator;
print "<br><br>";
while( (my $OPERATION, my $TITLE) = $item_iterator->() )
{
print "my $OPERATION, my $TITLE<be>";
}
}
我得到的输出:
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
}
}