B
Brad Baxter
Greetings,
I would like to take a table of indented text like the following:
AAA
BBB
CCC
DDD
EEE
FFF
GGG
.... and convert it into a nested array of arrays like the following:
[
[ 'AAA' => [
'BBB',
'CCC',
] ],
[ 'DDD' => [
[ 'EEE' => [
'FFF',
] ],
] ],
'GGG',
]
That is, if an element $a is an array ref, then $a->[0] is the text for
that level and $a->[1] is an array ref to the next level(s). Otherwise $a
is the text for that level.
The indenting is regular, the levels may be arbitrarily deep, and no
double indenting is allowed, i.e., the following is disallowed:
AAA
BBB
CCC
DDD
The solution I've come up with so far is to create the following literal
string and eval it:
[
[ 'AAA' => [
[ 'BBB' => [
] ],
[ 'CCC' => [
] ],
] ],
[ 'DDD' => [
[ 'EEE' => [
[ 'FFF' => [
] ],
] ],
] ],
[ 'GGG' => [
] ],
]
This obviously is not exactly what I describe above, but I can make it
work. However, this solution seems rather draconian, and I feel certain
there is a much simpler answer.
Regards,
Brad
I would like to take a table of indented text like the following:
AAA
BBB
CCC
DDD
EEE
FFF
GGG
.... and convert it into a nested array of arrays like the following:
[
[ 'AAA' => [
'BBB',
'CCC',
] ],
[ 'DDD' => [
[ 'EEE' => [
'FFF',
] ],
] ],
'GGG',
]
That is, if an element $a is an array ref, then $a->[0] is the text for
that level and $a->[1] is an array ref to the next level(s). Otherwise $a
is the text for that level.
The indenting is regular, the levels may be arbitrarily deep, and no
double indenting is allowed, i.e., the following is disallowed:
AAA
BBB
CCC
DDD
The solution I've come up with so far is to create the following literal
string and eval it:
[
[ 'AAA' => [
[ 'BBB' => [
] ],
[ 'CCC' => [
] ],
] ],
[ 'DDD' => [
[ 'EEE' => [
[ 'FFF' => [
] ],
] ],
] ],
[ 'GGG' => [
] ],
]
This obviously is not exactly what I describe above, but I can make it
work. However, this solution seems rather draconian, and I feel certain
there is a much simpler answer.
Regards,
Brad