Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 9 of 9

Thread: table to string

  1. #1
    Join Date
    May 2005
    Posts
    30

    table to string

    I couldn't find how i could create a string from table elements, so I wrote this function

    Code:
    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

    Code:
    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/!

  2. #2
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    use Table.Concat()

    Code:
    myTable = {"one", "two", "three", "four"};
    
    sTable = Table.Concat(myTable, " ", 1, TABLE_ALL)
    Paragraph.SetText("Paragraph1", sTable)

  3. #3
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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

    Code:
    myTable = {"one", "two", "three", "four"};
    
    myString = tableToString(myTable, " ");
    Paragraph.SetText("Paragraph1", myString);
    Code:
    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

  4. #4
    Join Date
    May 2005
    Posts
    30
    Quote Originally Posted by Worm View Post
    use Table.Concat()

    Code:
    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

  5. #5
    Join Date
    May 2005
    Posts
    30
    Quote Originally Posted by Worm View Post
    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

    Code:
    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.

  6. #6
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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.

  7. #7
    Join Date
    May 2005
    Posts
    30
    so once the string is retuned through the function, what do you call it?

  8. #8
    Join Date
    Jul 2002
    Location
    USA
    Posts
    3,959
    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.

    Code:
    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)

  9. #9
    Join Date
    May 2005
    Posts
    30
    ahhh.. i see. Thanks

Similar Threads

  1. Table from String?
    By nrgyzer in forum AutoPlay Media Studio 6.0
    Replies: 11
    Last Post: 05-23-2006, 11:01 PM
  2. String to Table Name
    By blazm in forum AutoPlay Media Studio 6.0
    Replies: 1
    Last Post: 03-20-2006, 01:42 PM
  3. string / table compare
    By gabrielfenwich in forum AutoPlay Media Studio 5.0
    Replies: 5
    Last Post: 01-29-2005, 11:30 PM
  4. Table x String
    By lanfz in forum AutoPlay Media Studio 5.0
    Replies: 2
    Last Post: 05-21-2004, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts