View Full Version : table to string
ZenLunatic
09-26-2006, 10:41 AM
I couldn't find how i could create a string from table elements, so I wrote this function
function tableToString(TABLE, STRING, SEPARATOR)
tableLength = Table.Count(TABLE);
--create blank string
STRING = "";
for count = 1, tableLength do
--add table element to string and insert a space at the end
STRING = STRING..TABLE[count].." ";
end
return STRING;
end
It seems like it should work, but when I try to use it with this
myTable = {"one", "two", "three", "four"};
tableToString(myTable, myString, " ");
Paragraph.SetText("Paragraph1", myString);
I get an error that myString isn't a string.
any ideas/tips/help/!
use Table.Concat()
myTable = {"one", "two", "three", "four"};
sTable = Table.Concat(myTable, " ", 1, TABLE_ALL)
Paragraph.SetText("Paragraph1", sTable)
To answer the question about the function. You are returning the value, so your function call should be set to a variable to recieve the returned value
myTable = {"one", "two", "three", "four"};
myString = tableToString(myTable, " ");
Paragraph.SetText("Paragraph1", myString);
function tableToString(TABLE, SEPARATOR)
tableLength = Table.Count(TABLE);
--create blank string
STRING = "";
for count = 1, tableLength do
--add table element to string and insert a space at the end
STRING = STRING..TABLE[count].." ";
end
return STRING;
end
ZenLunatic
09-26-2006, 12:10 PM
use Table.Concat()
myTable = {"one", "two", "three", "four"};
sTable = Table.Concat(myTable, " ", 1, TABLE_ALL)
Paragraph.SetText("Paragraph1", sTable)
That works.. I didn't even look at it since I thought it was a function to concat two tables!
thanks
ZenLunatic
09-26-2006, 12:13 PM
To answer the question about the function. You are returning the value, so your function call should be set to a variable to recieve the returned value
function tableToString(TABLE, SEPARATOR)
tableLength = Table.Count(TABLE);
--create blank string
STRING = "";
for count = 1, tableLength do
--add table element to string and insert a space at the end
STRING = STRING..TABLE[count].." ";
end
return STRING;
end
With that function, however, the function could only be used once (unless you want to redeclare STRING) since the returned string will be named STRING in all uses of the function. That's why I had a spot to name the string in the original function.
Not so. You're initializing STRING to nothing, (STRING = "";) in your function. That clears the variable from any previouse values. You could also use:
local STRING
which keeps the variable from being Global and also only keeps the values for that instance of the function.
ZenLunatic
09-26-2006, 12:39 PM
so once the string is retuned through the function, what do you call it?
when you return a value from a function, you use a variable to hold the returned values. Similar to the the way most of AMS actions are. You can return a table, a string, a number, even multiple variables.
function Sample()
return "ABC"
end
--sample function call
sMyVariable = Sample()
--sMyVariable will be equal to the returned value of the function
--in this case "ABC"
Dialog.Message("Test", sMyVariable)
ZenLunatic
09-26-2006, 12:49 PM
ahhh.. i see. Thanks :)
Powered by vBulletin™ Version 4.0.6 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.