Page 1 of 1

[26 Apr 2009] Converting special string into a table

Posted: Sat Jun 13, 2020 5:32 pm
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

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

Posted: Sat Jun 13, 2020 5:32 pm
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 :)