Parsing tab formatted hierarchical strings into a tree

Hello all.
I have this tab formatted hierarchical structure.
--------------------
0 A
1 B
2 C
2 D
1 E
--------------------

Every line has a string intended to its depth with tabs. (The numbers
in the above example are the number of tabs intending that line and
are not present in the actual data)

I need to parse the above structure into a tree (multi-dimensional
array), for eg:
Array
(
[A] => Array
(
[B] => Array
(
[C] =>
[D] =>
)
[E] =>
)
)

I have this loop that parses each line and has the following for every
line,
$depth = (number of tabs) and $key = (A, B etc..)

So the string in each line is the KEY of the array / tree. Needn't
worry about the value.

This is pretty simple, but I just can't get my head around it. Could
anyone throw some light on this issue?

Thank you.

--
Kailash Nadh | http://kailashnadh.name
Kailash Nadh [ Mo, 28 Januar 2008 00:03 ] [ ID #1917360 ]

Re: Parsing tab formatted hierarchical strings into a tree

Kailash Nadh wrote:
> Hello all.
> I have this tab formatted hierarchical structure.
> --------------------
> 0 A
> 1 B
> 2 C
> 2 D
> 1 E
> --------------------
>
> Every line has a string intended to its depth with tabs. (The numbers
> in the above example are the number of tabs intending that line and
> are not present in the actual data)
>
> I need to parse the above structure into a tree (multi-dimensional
> array), for eg:
> Array
> (
> [A] => Array
> (
> [B] => Array
> (
> [C] =>
> [D] =>
> )
> [E] =>
> )
> )
>
> I have this loop that parses each line and has the following for every
> line,
> $depth = (number of tabs) and $key = (A, B etc..)
>
> So the string in each line is the KEY of the array / tree. Needn't
> worry about the value.
>
> This is pretty simple, but I just can't get my head around it. Could
> anyone throw some light on this issue?
>
> Thank you.
>
> --
> Kailash Nadh | http://kailashnadh.name
>


Personally, I'd use a recursive function. Parameters to the function
are reference to a variable and depth (first is an empty array and 0).

Read a new item. If it is on same level, just add it to the current
array. If it's one level lower, add an array as the new element and
recurse with the new array as the parameter.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Mo, 28 Januar 2008 02:16 ] [ ID #1918240 ]

Re: Parsing tab formatted hierarchical strings into a tree

On Jan 28, 1:16 am, Jerry Stuckle <jstuck... [at] attglobal.net> wrote:
> KailashNadhwrote:
> > Hello all.
> > I have this tab formatted hierarchical structure.
> > --------------------
> > 0 A
> > 1 B
> > 2 C
> > 2 D
> > 1 E
> > --------------------
>
> > Every line has a string intended to its depth with tabs. (The numbers
> > in the above example are the number of tabs intending that line and
> > are not present in the actual data)
>
> > I need to parse the above structure into a tree (multi-dimensional
> > array), for eg:
> > Array
> > (
> > [A] => Array
> > (
> > [B] => Array
> > (
> > [C] =>
> > [D] =>
> > )
> > [E] =>
> > )
> > )
>
> > I have this loop that parses each line and has the following for every
> > line,
> > $depth = (number of tabs) and $key = (A, B etc..)
>
> > So the string in each line is the KEY of the array / tree. Needn't
> > worry about the value.
>
> > This is pretty simple, but I just can't get my head around it. Could
> > anyone throw some light on this issue?
>
> > Thank you.
>
> > --
> >KailashNadh|http://kailashnadh.name
>
> Personally, I'd use a recursive function. Parameters to the function
> are reference to a variable and depth (first is an empty array and 0).
>
> Read a new item. If it is on same level, just add it to the current
> array. If it's one level lower, add an array as the new element and
> recurse with the new array as the parameter.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck... [at] attglobal.net
> ==================

I already thought of using a recursive function, but what would happen
if there's a case like this ?
0|
1| A
2| B
3| C
4| D
4| E
5| F
6| G
6| H
2| I
2| H
3| K

After level 6, $depth suddenly jumps back to 2 and goes to 3 and so
on.

Thank you.

--
Kailash Nadh | http://kailashnadh.name
Kailash Nadh [ Mo, 28 Januar 2008 19:26 ] [ ID #1918298 ]

Re: Parsing tab formatted hierarchical strings into a tree

Kailash Nadh wrote:
> On Jan 28, 1:16 am, Jerry Stuckle <jstuck... [at] attglobal.net> wrote:
>> KailashNadhwrote:
>>> Hello all.
>>> I have this tab formatted hierarchical structure.
>>> --------------------
>>> 0 A
>>> 1 B
>>> 2 C
>>> 2 D
>>> 1 E
>>> --------------------
>>> Every line has a string intended to its depth with tabs. (The numbers
>>> in the above example are the number of tabs intending that line and
>>> are not present in the actual data)
>>> I need to parse the above structure into a tree (multi-dimensional
>>> array), for eg:
>>> Array
>>> (
>>> [A] => Array
>>> (
>>> [B] => Array
>>> (
>>> [C] =>
>>> [D] =>
>>> )
>>> [E] =>
>>> )
>>> )
>>> I have this loop that parses each line and has the following for every
>>> line,
>>> $depth = (number of tabs) and $key = (A, B etc..)
>>> So the string in each line is the KEY of the array / tree. Needn't
>>> worry about the value.
>>> This is pretty simple, but I just can't get my head around it. Could
>>> anyone throw some light on this issue?
>>> Thank you.
>>> --
>>> KailashNadh|http://kailashnadh.name
>> Personally, I'd use a recursive function. Parameters to the function
>> are reference to a variable and depth (first is an empty array and 0).
>>
>> Read a new item. If it is on same level, just add it to the current
>> array. If it's one level lower, add an array as the new element and
>> recurse with the new array as the parameter.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck... [at] attglobal.net
>> ==================
>
> I already thought of using a recursive function, but what would happen
> if there's a case like this ?
> 0|
> 1| A
> 2| B
> 3| C
> 4| D
> 4| E
> 5| F
> 6| G
> 6| H
> 2| I
> 2| H
> 3| K
>
> After level 6, $depth suddenly jumps back to 2 and goes to 3 and so
> on.
>
> Thank you.
>
> --
> Kailash Nadh | http://kailashnadh.name
>

No problem. If the depth is lower than the current one, return to
higher levels until you have the correct depth.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex [at] attglobal.net
==================
Jerry Stuckle [ Mo, 28 Januar 2008 23:40 ] [ ID #1918320 ]
PHP » comp.lang.php » Parsing tab formatted hierarchical strings into a tree

Vorheriges Thema: Best way to output HTML from PHP
Nächstes Thema: Getting Paid from Abroad as a PHP Developer