[Add Menu]
Name = L1A
Title = L1A
[Done]
[Add Entry]
Action = None
Text = L2A
[Done]
[Add Entry]
Action = None
Text = Second
[Done]
-----------------------
[Add Menu]
Name = L2A
Title = L1A - L2A
[Done]
[Add Entry]
Action = None
Text = L3A
[Done]
[Add Entry]
Action = None
Text = L3B
[Done]
-------------------------
[Add Menu]
Name = Second
Title = L1A - Second
[Done]
[Add Entry]
Action = exe command
Text = Third1
[Done]
[Add Entry]
Action = exe command
Text = L3B
[Done]
-------------------------
[Add Menu]
Name = L3A
Title = L1A - L2A - L3A
[Done]
[Add Entry]
Action = exe command
Text = L4A
[Done]
[Add Entry]
Action = exe command
Text = L3B1
[Done]
-------------------------
[Add Menu]
Name = L3B
Title = L1A - L2A - L3B
[Done]
[Add Entry]
Action = exe command
Text = L4A
[Done]
-------------------------
etc.. etc..
####
$VAR1 = {
'L1A' => {
'L2A' => {
'L3A' => {
'L4A' => 'exe command',
'L3B1' => 'exe command'
},
'L3B' => {
'L4A' => 'exe command'
}
},
'Second' => {
'L3B' => 'exe command',
'Third1' => 'exe command'
}
}
};
####
$VAR1 = [
{
'Counter' => 1,
'Action' => 'None',
'Name' => 'L1A',
'Text' => 'L2A',
'Title' => 'L1A'
},
{
'Counter' => 2,
'Action' => 'None',
'Name' => 'L1A',
'Text' => 'Second',
'Title' => 'L1A'
},
{
'Counter' => 3,
'Action' => 'None',
'Name' => 'L2A',
'Text' => 'L3A',
'Title' => 'L1A - L2A'
},
{
'Counter' => 4,
'Action' => 'None',
'Name' => 'L2A',
'Text' => 'L3B',
'Title' => 'L1A - L2A'
},
{
'Counter' => 5,
'Action' => 'exe command',
'Name' => 'Second',
'Text' => 'Third1',
'Title' => 'L1A - Second'
},
{
'Counter' => 6,
'Action' => 'exe command',
'Name' => 'Second',
'Text' => 'L3B',
'Title' => 'L1A - Second'
},
{
'Counter' => 7,
'Action' => 'exe command',
'Name' => 'L3A',
'Text' => 'L4A',
'Title' => 'L1A - L2A - L3A'
},
{
'Counter' => 8,
'Action' => 'exe command',
'Name' => 'L3A',
'Text' => 'L3B1',
'Title' => 'L1A - L2A - L3A'
},
{
'Counter' => 9,
'Action' => 'exe command',
'Name' => 'L3B',
'Text' => 'L4A',
'Title' => 'L1A - L2A - L3B'
}
];
####
$Menu_Multi_Hash{'L1A'}{'L2A'} = 'none';
etc.. etc.. and for the last one, it will be
$Menu_Multi_Hash{'L1A'}{'L2A'}{'L3B'}{'L4A'} = 'exe command';
(please note that the first three elements are the ones arrived by splitting the 'Title', the fourth is the 'Text' and the right hand side is the 'Action')
####
use Data::Dumper;
use strict;
open (FILE, "filename");
my $menu_counter = 0;
my $MenuName;
my $MenuTitle;
my %temp_hash;
my $line_read;
my @MenuList; #Array of Hashes
while () {
next if /^\s*$/;
if ( /^\[Add Menu]/../^\[Done]/) {
$line_read = $_;
if ( $line_read=~/^\s*(\w+)\s=\s(.*)/ ) {
if ($1 eq "Name") {
$MenuName = $2;
}
elsif ($1 eq "Title") {
$MenuTitle = $2;
}
}
}
elsif ( /^\[Add Entry]/../^\[Done]/) {
$line_read = $_;
if ( $line_read=~/^\s*(\w+)\s=\s(.*)/ ) {
if ( $1 eq "Action") {
if ($menu_counter != 0) {
push @MenuList, {%temp_hash};
}
$menu_counter++;
%temp_hash = &initialize_structure( $1, $2, $menu_counter, $MenuName, $MenuTitle );
}
else {
&createpair( $1, $2);
}
}
}
}
push @MenuList, {%temp_hash};
print Dumper(\@MenuList);
sub initialize_structure() {
my ( $key, $value, $counter, $menuname, $menutitle) = @_;
my %temp_var;
%temp_var = ( Name => "",
Title => "",
Action => "",
Text => "",
Counter => "",
);
$temp_var{$key} = $value;
$temp_var{'Counter'} = $counter;
$temp_var{'Name'} = $menuname;
$temp_var{'Title'} = $menutitle;
return %temp_var;
};
# subroutine to populate rest of the data structure
sub createpair() {
my($key, $value) = @_;
$temp_hash{$key} = $value;
};