Urgent : Can any1 solve my problem?

Status
Not open for further replies.

srbharadwaj

Broken In
Hi All,
This is the content of the file say 'input.txt'

Code:
apple   (
   ball  (
cat (
doctor(),
elephant()
),
cup (
dead()
)
));
As you can see its in a very haphazard manner, which is a big problem, but if you indent it properly it looks something like this

Code:
apple(
    ball(
        cat (
            doctor(),
            elephant()
        ),
        cup (
            dead()
        )
    )
);
Now what i reqire is the tree path for the last element example say for 'doctor' the tree-path is

Code:
apple.ball.cat.doctor()
and that of 'elephant' is

Code:
apple.ball.cat.elephant()
and that of 'dead' is
Code:
apple.ball.cup.dead()
';' indicates end of the path

can any automate this using any programming lang its very urgent :-?
Thanks
 

QwertyManiac

Commander in Chief
Do all the files have only one name(...); structure?

I've not spent much time covering up all extra conditions but for the example you have given, which has only one of those structures, this following code in Python prints it properly to an outfile.

Code:
Code:
import sys
ofile = open(sys.argv[1]+"out", 'w')
def search(tree, path='', pathlen=0):
    for each in tree:
        path += each+"."
        if tree[each]:
            search(tree[each], path)
        else:
            ofile.write(path[:-1]+'()'+'\n')
        path = path[:path[:-1].rfind('.')+1]

ip = open(sys.argv[1]).read().replace(' ','').replace('\n','')
stack = []
level = 0
result = {}
x = ip[:ip.find('(')]
ip = ip[ip.find('(')+1:-2]
result[x] = {}
ref = result[x]

for index, each in enumerate(ip):
    if each == '(':
        element = "".join(stack)
        stack = []
        ref[element] = {}
        if ip[index+1] != ')':  
            skip = True      
            save = ref
            ref = ref[element]
        continue
    if each == ')':
        if not skip:
            ref = save
        skip = False
        continue
    if each == ',':
        continue
    stack.append(each)

search(result)

Output:
Code:
harsh@home-desktop:~$ python3.0 a.py file.txt 
harsh@home-desktop:~$ cat file.txtout 
apple.ball.cup.dead()
apple.ball.cat.elephant()
apple.ball.cat.doctor()

Code is partially hackish, but I'll fix that when I get free again. But for single structures its bound to work perfectly.
 
OP
S

srbharadwaj

Broken In
yes every file will have just one name(); structure
thank a lot harsh....:)

Harsh, can u pls brief me abt the logic used to find out the last element?
 
Last edited:

QwertyManiac

Commander in Chief
I just checked if the next element after '(', in the space-and-newline stripped file, is a ')'. If that became true, I did not add a new node to the tree and preserved the pointer (references actually, in Python).

Simply speaking, my program sees your file like this "apple(ball(cat(doctor(),elephant()),cup(dead())));" and I looked for '()'. Hackish, ugh. But did your job so its cool. I'm lazy. :p

By the way, why not use XML to store such data? Life could be so much easier following standards!
 
Last edited:
Status
Not open for further replies.
Top Bottom