473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jan 27 '08 #1
3 2807
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.
js*******@attglobal.net
==================

Jan 28 '08 #2
On Jan 28, 1:16 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
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...@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
Jan 28 '08 #3
Kailash Nadh wrote:
On Jan 28, 1:16 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
>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...@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.
js*******@attglobal.net
==================

Jan 28 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
6
by: Tuang | last post by:
I've been looking all over in the docs, but I can't figure out how you're *supposed* to parse formatted strings into numbers (and other data types, for that matter) in Python. In C#, you can say...
4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
1
by: jayson_13 | last post by:
Hi, I want to retrieve the hierarchical structure which is store inside the database and then display it using treeview control. But now I got a design issue which I hope that you guys can help...
5
by: clintonG | last post by:
I'm looking for documentation and would not turn my nose up to any code from anybody who thinks they are good at the design of an algorythm that can be used to generated a hierarchical relational...
30
by: Vadim Tropashko | last post by:
Reposting with more clarification (as Jan asked). Suppose I have a BNFgrammar and a source text parsed into a tree. How would I query an identifier declaration? All the XQuery tutorials...
5
by: Svenn Are Bjerkem | last post by:
On Jul 23, 1:03 pm, christopher.saun...@durham.ac.uk (c d saunter) wrote: As a start I want to parse VHDL which is going to be synthesised, and I am limiting myself to the entities and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.