[26 Apr 2009] Converting special string into a table

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
User avatar
Clyde
Posts: 351
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

[26 Apr 2009] Converting special string into a table

Post by Clyde »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Sun, 26 Apr 2009 14:29:23 +0200

Hi there!

Ok, what I am looking for is an easy way to do the following: I have a string, that has the structure of a table, like

Code: Select all

string$ = "{1, 2, {3 , 4}}"
Now, what would be cool and what I need would be this:

Code: Select all

table = {}
table = string$
:-)

This means, is there a way that the content of the string becomes the table? So, if I would write table[1] I would get the value 2? I am not sure, if maybe WriteTable() and ReadTable() could be used for that as a workaround!?

Thanks in advance, Micha
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
User avatar
airsoftsoftwair
Posts: 5914
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[27 Apr 2009] Re: Converting special string into a table

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 27 Apr 2009 01:04:29 +0200
Hi there!

Ok, what I am looking for is an easy way to do the following: I have a string, that has the structure of a table, like

Code: Select all

string$ = "{1, 2, {3 , 4}}"
Now, what would be cool and what I need would be this:

Code: Select all

table = {}
table = string$
:-)

This means, is there a way that the content of the string becomes the table? So, if I would write table[1] I would get the value 2? I am not sure, if maybe WriteTable() and ReadTable() could be used for that as a workaround!?
No, you can't use WriteTable() for this because it needs a table and not a string as argument.

There's no way to do this unless you write a little function that does the job. It's not very hard... just parse the string and whenever you encounter a brace, you recurse into the next table level. Because table management is very easy in Hollywood, such a recursive function could be written in 15 minutes or so :)
Locked