P
Pat
Hi,
This seems like something that would be a FAQ, but I couldn't find an
answer there.
I would like to build a hash from an input text file. The hash can
contain other hashes, and so on, to any depth. The format of the input
file can be anything that is convenient, e.g.:
---------------------------------
root
{
branch_1
{
sub_branch_1
{
leaf_1 = "This is leaf_1"
leaf_2 = "This is leaf_2"
}
}
branch_2
{
leaf_3 = "This is leaf_3"
}
leaf_4 = "This is leaf_4"
}
---------------------------------
I would like for that input file to generate a hash equivalent to what
this perl code would:
%tree{'root'}{'branch_1'}{'sub_branch_1'}{'leaf_1'} = "This is leaf_1";
%tree{'root'}{'branch_1'}{'sub_branch_1'}{'leaf_2'} = "This is leaf_2";
%tree{'root'}{'branch_2'}{'leaf_3'} = "This is leaf_3";
%tree{'root'}{'leaf_4'} = "This is leaf_4";
As a first attempt, I tried writing a recursive subroutine that would try
to match pairs of open/close curly braces, then call itself on everything
between the braces. But I couldn't get the right amount of "greediness"
in the matching.
Any help appreciated.
Pat
This seems like something that would be a FAQ, but I couldn't find an
answer there.
I would like to build a hash from an input text file. The hash can
contain other hashes, and so on, to any depth. The format of the input
file can be anything that is convenient, e.g.:
---------------------------------
root
{
branch_1
{
sub_branch_1
{
leaf_1 = "This is leaf_1"
leaf_2 = "This is leaf_2"
}
}
branch_2
{
leaf_3 = "This is leaf_3"
}
leaf_4 = "This is leaf_4"
}
---------------------------------
I would like for that input file to generate a hash equivalent to what
this perl code would:
%tree{'root'}{'branch_1'}{'sub_branch_1'}{'leaf_1'} = "This is leaf_1";
%tree{'root'}{'branch_1'}{'sub_branch_1'}{'leaf_2'} = "This is leaf_2";
%tree{'root'}{'branch_2'}{'leaf_3'} = "This is leaf_3";
%tree{'root'}{'leaf_4'} = "This is leaf_4";
As a first attempt, I tried writing a recursive subroutine that would try
to match pairs of open/close curly braces, then call itself on everything
between the braces. But I couldn't get the right amount of "greediness"
in the matching.
Any help appreciated.
Pat