my @menu_bar_names = qw ( aMenu bMenu ... );
my @MenuItems="";
my %aMenu = { label => " A",
MenuItems => [ @MenuItems ] };
my %bMenu = { label => " B",
MenuItems => [ @MenuItems ] };
my %cMenu = { label => " C",
MenuItems => [ @MenuItems ] };
I used the following to populate each MenuItem array.
foreach my $app (@sorted_applications) {
$app = lc $app;
if ( "$app" lt "b" ) {
push ( @{ $aMenu{MenuItems} }, $app);
next;
} elsif ($app lt "c" ) {
push (@{ $bMenu{MenuItems} },$app);
next;
...
And the following to print the first element.
print " @{$aMenu{MenuItems}}[0]\n";
I would like to avoid using $aMenu in the printing and use something like, $abc_menu instead of hard coding $aMenu, $bMenu such as;
foreach my $abc_menu (@menu_bar_names) {
print " @{$abc_menu{MenuItems}}[0]\n";
}
but I get the following error. 'Global symbol "abc_menu"; requires explicit package name at...' The error makes sense and I understand why I get it. But I haven't figured out how to get around it. I'm sure it's something simple but I've read so much I've confused myself. Thanks for taking the time to read this.
my %Menu = {
app1 => {
label => " A",
MenuItems => [ @MenuItems ]
},
app2 => {
label => " B",
MenuItems => [ @MenuItems ]
},
app3 => {
label => " C",
MenuItems => [ @MenuItems ]
},
};
Then replace other mentions of aMenu, etc. with Menu{lc_app}. For example:
foreach my $app (@sorted_applications) {
$app = lc $app;
push @{ $Menu{$app}{MenuItems} }, $app;
}
Phil
my %aMenu = { label => " A", MenuItems => [ @MenuItems ] };
And should be (warnings would have complained about it):
my %aMenu = ( label => " A", MenuItems => [ @MenuItems ] );
# if @MenuItems actually has something it it, then MenuItems => [@MenuItems] is fine
# but for simplicity i just made it MenuItems => []
my %menus = ( a => { label => " A", MenuItems => [] },
a => { label => " A", MenuItems => [] },
a => { label => " A", MenuItems => [] },
);
#OR: my %menus = map { lc($_) => {label => " $_", MenuItems => []} } 'A' .. 'C';
# then:
foreach my $app (map {lc $_} @sorted_applications) {
push @{ $menus{$app}->{MenuItems} }, $app;
}
Use the menu letter as the key and defer creation of hash entries until they are needed cleans things up a little. Consider:
use strict;
use warnings;
my %menus;
my @sorted_applications = qw(app1 App2 bapp bopp cap cop);
for my $app (@sorted_applications) {
# Create and populate menus
$app = lc $app;
my $letter = substr $app, 0, 1;
if (! exists $menus{$letter}) {
#create the menu entry
$menus{$letter}{label} = '$nbsp; ' . uc $letter;
$menus{$letter}{items} = []; # Not required, but shows intent
}
push @{$menus{$letter}{items}}, $app;
}
# Dump the menu structure
for my $topMenu (sort keys %menus) {
print "$menus{$topMenu}{label}\n";
print " $_\n" for @{$menus{$topMenu}{items}};
}
Prints:
$nbsp; A app1 app2 $nbsp; B bapp bopp $nbsp; C cap cop
perlmonks.org content © perlmonks.org and davidrw, GrandFather, philcrow, plmc
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03