Home Technical Talk

MaxScript, Array, save and reload

Hello, I have a little question about arrays on which I am quite stuck on.

I have an array.

#("BoxA", "BoxB")

I manage to save this array in an external file, a txt file. My array in here will then look like this :

BoxA,BoxB,

Here now my issue. I want to reload this array back into my script, but for now I only succed to reload 1 of the component of my array !.
I am unable to find back my #("BoxA", "BoxB") from the BoxA,BoxB, saved in the file ! I have try so many diferent tricks that I cant tell them all in here...
Someone could at least point to the good direction ?

Thanks !

Replies

  • poopipe
    Options
    Offline / Send Message
    poopipe grand marshal polycounter
    maxscript's text handling is a bit weak but there's no reason on earth why you couldn't do this

    have another look through the textstream/parsing stuff in the reference - there'll be something you can use
  • SyncViewS
    Options
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi,
    this should do the trick. I suggest to make a function out of it.
    (
        -- Open the FileStream to read from disc
        local theFileStream = openFile "C:/test.txt"
    
        -- Initialize an empty Array to store the result
        local asWords = #()
    
        -- Enter a while loop that terminates once the full file has been read
        while ((eof theFileStream) == false) do
        (
            -- Read one set of characters delimited by a comma...
            local sWord = readDelimitedString theFileStream ","
    
            -- ...And append it to the result array
            append asWords sWord
        )
    
        -- Close the FileStream
        close theFileStream
    
        -- Cleanup memory used by the FileStream
        free theFileStream
    
        -- Display the resulting Array in the Listener
        format "asWords: %\n" asWords
    )
    

    Cheers
  • Blob
    Options
    Offline / Send Message
    Thanks a lot, this is working great.
    Looks like I was completly wrong in my way to loop in the file.

    Thanks ! : )
  • monster
    Options
    Offline / Send Message
    monster polycounter
    Another method would be to save your array to the file like this:
    format "%\n" (MyArray as string) to:MyFile
    
    Then load it like this:
    MyArray = execute(readline MyFile)
    
  • McGreed
    Options
    Offline / Send Message
    McGreed polycounter lvl 15
    You could also try saving the file as a INI file instead. :) Look up INI save load in the documentation. You can make sections as well. I'm currently using INI files for storing a lot of data.
  • Blob
    Options
    Offline / Send Message
    monster : Looks like an even more simple solution for me, So if I understand well my array will be save just as it is as a string, then to load it I wont have anymore to do any loop in the file, just execute it as it is. (I will test all that very soon :] ).

    McGreed : those ini files looks very interesting indeed, thanks for the information ! :]
  • McGreed
    Options
    Offline / Send Message
    McGreed polycounter lvl 15
    For splitting text strings into Arrays, I usually use the character ¦ as seperator, since it never seem to be used for anything normally, and not something you would use in for example a filename.

    a = filterString "bibot¦Tiktika¦string1¦23¦true"
    print a[3] -- gives "string1"
Sign In or Register to comment.