LuaTables and how to store it to file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Jonas DK
    Indigo Rose Customer
    • Jul 2004
    • 345

    LuaTables and how to store it to file?

    Hi all,

    It's nice to be back in AMS.
    Im a bit shaken by discovering how rusty your skills get when you don't use em for years.

    Can some one help me get my head around this problem...

    I am creating a small app that my students should make and we are working on tables and nested tables.

    So I have created the follow function:
    Code:
    function createDB()
    	tblData = {};
    	sPassword = Dialog.PasswordInput("Password", "Dit Password:", MB_ICONQUESTION);
    	md5Password = Crypto.MD5DigestFromString(enCrypt(sPassword));
    	tblVerify = {Pass=md5Password};
    	tblData[1] = tblVerify
    	Dialog.Message("md5 Hash", tblData[1].Pass);
    	TextFile.WriteFromTable(pMyDocuments.."\\AMS_Test.txt", tblData, false);
    	
    end
    (It's a smaal app they can use to store there homework assignments in, so it it going to get more tables inside the main table)

    Eatch table wil have 4 values (lets call them value1, value2, value3 and value4)

    so the main table is tblData
    and eatch set of data is written into the main table (sort of the same way as the Employee sample in the help files.

    And here is the problem. I want to save the tblData Table to file when done working with it and reading it back into the app when it opens.

    naive as I am I used the textfile.wrtiefromtable function, but when I look into the textfile after the application exits, the file is empty.

    What is my problem?

    my first thourght is that is could have something to do with the Textfile.WriteFromTable function, that it cannot see the data in the nested tables and therefore saves the empty file?

    Is there another way to do this so I can save and read the file?
    I specifically does not want to use a database (we are saving that for when the app is finished to have a session on code optimisation).


    - Jonas
  • Shrek
    Forum Member
    • Jul 2012
    • 723

    #2
    I think its just the way your accessing the tables (cant see how you created them though):

    Comment

    • Jonas DK
      Indigo Rose Customer
      • Jul 2004
      • 345

      #3
      Originally posted by Shrek View Post
      I think its just the way your accessing the tables (cant see how you created them though):

      http://www.indigorose.com/forums/thr...478#post114478
      Well the problem is not in using nested tables, but more how to store the data to file.

      but thanks for the tip anyway

      -Jonas

      Comment

      • Ulrich
        Indigo Rose Staff Member
        • Apr 2005
        • 5131

        #4
        Originally posted by Jonas DK View Post
        naive as I am I used the textfile.wrtiefromtable function, but when I look into the textfile after the application exits, the file is empty.
        What is my problem?
        "textfile.wrtiefromtable" is not a valid action part of AutoPlay Media Studio. Neither is it properly capitalized, nor is that the name of the action which exists. Who knows what other errors you made in your code? We can't know. Perhaps you can attach a sample project with the essential code you wrote, so we can see why the textfile is empty (when it shouldn't be).

        Ulrich

        Comment

        • Jonas DK
          Indigo Rose Customer
          • Jul 2004
          • 345

          #5
          Originally posted by Ulrich View Post
          "textfile.wrtiefromtable" is not a valid action part of AutoPlay Media Studio. Neither is it properly capitalized, nor is that the name of the action which exists. Who knows what other errors you made in your code?

          Ulrich
          I'm sorry for the sloopyness of the end of my post, thourght since it was in the code block of the post people would get the drift...

          But I stand corrected. Here is the completely and otterly Correct syntax :
          TextFile.WriteFromTable(FileToWrite, TableToReadFrom, AppendOrNot);

          In replay to Ulrich and Shrek I will repost the same code as above, since it seams to have gotten lost in the post
          Dont mind that this is a function, I just like to have my code in functions. This is the code that is run.
          When the app starts i have createDB(); on the startup section.

          If you run this in AMS
          you will get a dialog with an input, the text you input here er then passed through crypto
          to my enCrypt() function that just takes the string and encrypts it with blowfish and returns it to the MD5digest

          Then a table is created to hold the md5 digest and the table is nested inside of the tblData table
          then you get another dialog that shows you the content of the pass field in the nested table inside tblData (should be the md5 hash).

          tblData is then passed to TextFile.WriteFromTable() and that creates a file called AMS_Test.txt in my Documents folder
          and then the application exits.

          If I open the textfile AMS_Test.txt in Notepad++ it is empty (= nothing, no md5 digest or table, not even a blank line)

          Hopefully someone can shead some light on this, is it that I don't have a completely flat table ? but have nested a table into the tblData table? if that is it then this is correct behavier of the TextFile.WriteFromTable() because tblData Holds no values other then another table. if so would this implie that I should use an INI file or XML instead and how to build that so I can easily read it back in?

          function createDB()
          tblData = {};
          sPassword = Dialog.PasswordInput("Password", "Dit Password:", MB_ICONQUESTION);
          md5Password = Crypto.MD5DigestFromString(enCrypt(sPassword));
          tblVerify = {Pass=md5Password};
          tblData[1] = tblVerify
          Dialog.Message("md5 Hash", tblData[1].Pass);
          TextFile.WriteFromTable(pMyDocuments.."\\AMS_Test. txt", tblData, false);

          end

          -- Jonas

          Edit: Sory just read this and think it ended up with a negative undertone that was not intended, hopefully no one will take affence to this if so I'm truly sorry.
          Last edited by Jonas DK; 01-21-2013, 05:50 PM. Reason: sounding negative

          Comment

          • Ulrich
            Indigo Rose Staff Member
            • Apr 2005
            • 5131

            #6
            The documentation for TextFile.WriteFromTable() states that each index of the table needs to be string, in order to be able to flush the contents of the table to a text file. Your table does not contain strings - right now you are indexing tables. This is why your text file is empty.

            This works:

            Code:
            [COLOR="#0000FF"]function[/COLOR] createDB()
              tblData [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{};[/COLOR]
              sPassword [COLOR="#FF0000"]=[/COLOR] Dialog[COLOR="#FF0000"].[/COLOR]PasswordInput([COLOR="#800080"]"Password"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Dit Password:"[/COLOR][COLOR="#FF0000"],[/COLOR] MB_ICONQUESTION)[COLOR="#FF0000"];[/COLOR]
              [COLOR="#008000"]--md5Password = Crypto.MD5DigestFromString(enCrypt(sPassword));[/COLOR]
              md5Password [COLOR="#FF0000"]=[/COLOR] Crypto[COLOR="#FF0000"].[/COLOR]MD5DigestFromString(sPassword)[COLOR="#FF0000"];[/COLOR]
              sUsername [COLOR="#FF0000"]=[/COLOR] Dialog[COLOR="#FF0000"].[/COLOR]PasswordInput([COLOR="#800080"]"Username"[/COLOR][COLOR="#FF0000"],[/COLOR] [COLOR="#800080"]"Username:"[/COLOR][COLOR="#FF0000"],[/COLOR] MB_ICONQUESTION)[COLOR="#FF0000"];[/COLOR]
              tblVerify [COLOR="#FF0000"]=[/COLOR] [COLOR="#FF0000"]{[/COLOR]Pass[COLOR="#FF0000"]=[/COLOR]md5Password[COLOR="#FF0000"],[/COLOR]Username[COLOR="#FF0000"]=[/COLOR]sUsername[COLOR="#FF0000"]};[/COLOR]
              [COLOR="#0000FF"]local[/COLOR] sSerialize [COLOR="#FF0000"]=[/COLOR] [COLOR="#800080"]""[/COLOR][COLOR="#FF0000"];[/COLOR]
              [COLOR="#0000FF"]for[/COLOR] i[COLOR="#FF0000"],[/COLOR] k [COLOR="#0000FF"]in[/COLOR] [COLOR="#0000FF"]pairs[/COLOR] (tblVerify) [COLOR="#0000FF"]do[/COLOR]
                [COLOR="#0000FF"]if[/COLOR] [COLOR="#FF0000"]#[/COLOR]sSerialize [COLOR="#FF0000"]>[/COLOR] [COLOR="#000000"]0[/COLOR] [COLOR="#0000FF"]then[/COLOR]
                  sSerialize [COLOR="#FF0000"]=[/COLOR] sSerialize [COLOR="#FF0000"]..[/COLOR] [COLOR="#800080"]"|"[/COLOR][COLOR="#FF0000"];[/COLOR]
                [COLOR="#0000FF"]end[/COLOR]
                sSerialize [COLOR="#FF0000"]=[/COLOR] sSerialize [COLOR="#FF0000"]..[/COLOR] i[COLOR="#FF0000"]..[/COLOR][COLOR="#800080"]"="[/COLOR][COLOR="#FF0000"]..[/COLOR]k[COLOR="#FF0000"];[/COLOR]
              [COLOR="#0000FF"]end[/COLOR]
              tblData[COLOR="#FF0000"][[/COLOR][COLOR="#000000"]1[/COLOR][COLOR="#FF0000"]]=[/COLOR] sSerialize[COLOR="#FF0000"];[/COLOR]
              Dialog[COLOR="#FF0000"].[/COLOR]Message([COLOR="#800080"]"md5 Hash"[/COLOR][COLOR="#FF0000"],[/COLOR] tblData[COLOR="#FF0000"][[/COLOR][COLOR="#000000"]1[/COLOR][COLOR="#FF0000"]][/COLOR])[COLOR="#FF0000"];[/COLOR]
              TextFile[COLOR="#FF0000"].[/COLOR]WriteFromTable(pMyDocuments[COLOR="#FF0000"]..[/COLOR][COLOR="#800080"]"[COLOR="#800080"]\\[/COLOR]AMS_Test.txt"[/COLOR][COLOR="#FF0000"],[/COLOR] tblData[COLOR="#FF0000"],[/COLOR] [COLOR="#0000FF"]false[/COLOR])[COLOR="#FF0000"];[/COLOR]
              [COLOR="#0000FF"]error[/COLOR] [COLOR="#FF0000"]=[/COLOR] Application[COLOR="#FF0000"].[/COLOR]GetLastError()[COLOR="#FF0000"];[/COLOR]
              [COLOR="#0000FF"]if[/COLOR] ([COLOR="#0000FF"]error[/COLOR] [COLOR="#FF0000"]~=[/COLOR] [COLOR="#000000"]0[/COLOR]) [COLOR="#0000FF"]then[/COLOR]
                Dialog[COLOR="#FF0000"].[/COLOR]Message([COLOR="#800080"]"Error"[/COLOR][COLOR="#FF0000"],[/COLOR] _tblErrorMessages[COLOR="#FF0000"][[/COLOR][COLOR="#0000FF"]error[/COLOR][COLOR="#FF0000"]],[/COLOR] MB_OK[COLOR="#FF0000"],[/COLOR] MB_ICONEXCLAMATION)[COLOR="#FF0000"];[/COLOR]
              [COLOR="#0000FF"]end[/COLOR]
            [COLOR="#0000FF"]end[/COLOR]
            
            pMyDocuments [COLOR="#FF0000"]=[/COLOR] Shell[COLOR="#FF0000"].[/COLOR]GetFolder(SHF_MYDOCUMENTS)[COLOR="#FF0000"];[/COLOR]
            createDB()[COLOR="#FF0000"];[/COLOR]
            Of course this means that you need to parse the text file when you read it, and reconstruct the table with the index=value pairs.

            Ulrich

            Comment

            • Jonas DK
              Indigo Rose Customer
              • Jul 2004
              • 345

              #7
              Thanks Ulrich for the quick repley.

              I knew it was something to do with the indexing just wassent sure if there was an easy way around it or if it would be better to read it into some form of structure like INI or XML.

              Let me just make sure I understand this.

              You make the nested table into a string and place the string in the tblData with the delimiter |
              So to get it back into the nested table format you would use some string manupulation to create the table?

              Comment

              • Ulrich
                Indigo Rose Staff Member
                • Apr 2005
                • 5131

                #8
                You can use something like DelimitedStringToTable(), for example, to read the strings back to individual elements of a table.
                Another option would be using an INI file, creating a Section for each "record".

                Ulrich

                Comment

                Working...
                X