Indigo Rose Software

Professional Software Development Tools

 
+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 17

Thread: append to table

  1. #1
    Join Date
    Feb 2012
    Posts
    163

    append to table

    I want to append;

    "

    to the start of each table item and

    ",

    to the end of each item except the last item that gets just this;

    "

    is this possible?, if not is there another way to read a text file and append what's needed to each line then save it as a new file?

  2. #2
    Join Date
    Apr 2010
    Posts
    529
    Mick,
    There are a couple ways to handle this... and of course a couple questions

    Where does the table data come from and how do you get it into the table?

    Do you "have" to append the " and ", to each record or could you not append this when you read/use the table data?

    Can you not prepend/append the information then before adding it to the table?

    Otherwise you could iterate through the records in the table using a
    Code:
    for i, v in pairs(tblname) do
    ... and change each record.

    Cheers,
    MadDogDean

  3. #3
    Join Date
    Feb 2012
    Posts
    163
    Quote Originally Posted by MadDogDean View Post
    Where does the table data come from and how do you get it into the table?
    reading a text file to a table, the text is a list (no data so no delim)

    Do you "have" to append the " and ", to each record or could you not append this when you read/use the table data?
    It needs to be appended, I could append it when reading if you know how

    Can you not prepend/append the information then before adding it to the table?
    unfortunately not as I'll be making a lua script on the fly from computers not belonging to me, the "", form the new table generated from the text file.

  4. #4
    Join Date
    Apr 2010
    Posts
    529
    OK, so do you need to prepend/append the " and ", to every record at the same time? And what do you want to do with this data afterwards (what format or how will you use it)?

  5. #5
    Join Date
    Feb 2012
    Posts
    163
    Quote Originally Posted by MadDogDean View Post
    OK, so do you need to prepend/append the " and ", to every record at the same time? And what do you want to do with this data afterwards (what format or how will you use it)?
    messing about I've got the , part done but my data is now in a string;

    test,test,test,test,test

    I just need the string to look like this;

    "test","test","test","test","test"

    then I can save it to a text file and rename the .txt .lua so I have a script to call, dont really need to do it all in one action though as long as the end result is the same, thinking I

  6. #6
    Join Date
    Apr 2010
    Posts
    529
    Mick, good results so far

    If you want to append the " part, you need to do what you've done with the , but enquote it.

    For example:
    Code:
    strText = "test";
    strText = '"'..strText..'",';
    The text will now look like "test",

    The quoting is at at the beginning:
    single quote (apostrophe), double-quote, and another apostrophe
    The end quoting is
    single quote (apostrophe), double-quote, comma and another apostrophe.

    Alternatively you can escape the " by using the \

    Code:
    sText = "\""..strText.."\","
    ... but your brain will explode if you try too much of this!
    The single quote is much simpler!

    Cheers,
    MadDogDean
    Last edited by MadDogDean; 02-09-2012 at 10:19 PM.

  7. #7
    Join Date
    Feb 2012
    Posts
    163
    ^^^thanks

    OJ, so this works;

    Code:
    data = Table.Concat(convert, ";", 1, 1);
    strText = '"'..data..'",';
    so how would I loop it so it checks the rest of the table (just one column)?

  8. #8
    Join Date
    Apr 2010
    Posts
    529
    Mick,

    Try looping it like this:
    Code:
    tbl_original = {"1", "2", "3"} -- replace this with your original table
    
    tbl_temp = {}
    
    for i, v in pairs(tbl_original) do
    
    	v = '"'..v..'"'
    	Table.Insert(tbl_temp, i, v)
    end
    
    strNewString = Table.Concat(tbl_temp, ",", 1, TABLE_ALL);
    Cheers,
    MadDogDean
    Last edited by MadDogDean; 02-10-2012 at 02:20 PM.

  9. #9
    Join Date
    Feb 2012
    Posts
    163
    Code:
    Data2 = {};
    for index, value in pairs(Data) do
    Data2[index] = '"'..value..'",';
    end
    Count1 = Table.Count(Data2);
    Last = Table.Concat(Data2, ";", "" .. Count1 .. "", "" .. Count1 .. "");
    Delete = Table.Remove(Data2, "" .. Count1 .. "");
    Count2 = Table.Count(Data2);
    Last1 = Count2 + 1  --------- need to add 1 on to "Count2"-------------------
    Table.Insert(Data2, "" .. tableinsert .. "", '"'..Last..'"');
    TextFile.WriteFromTable(_DesktopFolder .."\\Script.txt", Data2, true);
    OK, got it working but I cant figure out how to do a basic add numbers so I can add the lasts item back to the table without the , I can find how to do everything math related but + -? or is there a way to add directly to the end of the table?

  10. #10
    Join Date
    Feb 2012
    Posts
    163
    @Dean, I was just writing post 9 as you were 8 lol, got it working for the loop, thanks

  11. #11
    Join Date
    Apr 2010
    Posts
    529
    Mick,

    I can find how to do everything math related but + -? or is there a way to add directly to the end of the table
    You could use Table.Insert and specify Table.Count(new_table)+1 (this adds it after the last entry) as the insert location. This inserts it at the end of the table.
    Code:
    Table.Insert(tbl_new, Table.Count(tbl_new)+1, value)
    But, it's simpler looping through than doing the remove... insert... count... etc, etc.

    Just remember that you can never "copy" a table by variable declaration/assignment, you have to actually "build" a new table if you want to duplicate the data in another copy.

    Table.Insert (Table,Position,Value)

    Code:
    new_tbl={}
    
    for i, v in pairs (old_table) do
        Table.Insert(new_tbl, i, v)
    end

    Cheers,
    MadDogDean

  12. #12
    Join Date
    Feb 2012
    Posts
    163
    thanks again

  13. #13
    Join Date
    Apr 2007
    Location
    Raalte, OV, Netherlands
    Posts
    3,287
    You could also use

    Code:
    tbl_new[#tbl_new+1] = value;
    instead of

    Code:
    Table.Insert(tbl_new, Table.Count(tbl_new)+1, value)
    in Lua 5.1+
    Bas Groothedde
    Imagine Programming :: Blog :: Familiar people here

    My AMS Plugins:

  14. #14
    Join Date
    Apr 2010
    Posts
    529
    Hey Bas,

    Clever! I like the smaller, compactness of the Lua 5.1 syntax you showed. Looks like time to do a little more read-up.

    Cheers,
    MadDogDean

  15. #15
    Join Date
    May 2006
    Posts
    1,443
    Quote Originally Posted by MadDogDean View Post
    Hey Bas,

    Clever! I like the smaller, compactness of the Lua 5.1 syntax you showed. Looks like time to do a little more read-up.

    Cheers,
    MadDogDean
    want to see more simple one ?
    Let the Lua to do work for you
    Code:
    table.insert(tbl,val)

Posting Permissions

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