Home Technical Talk

Another max script problem - array & external file

Is it possible to populate an array with information from a text file. The info in the text file would just be numbers on different lines. I've no idea how to do this, any help would be appreciated :)

Replies

  • cw
    Options
    Offline / Send Message
    cw polycounter lvl 17
    yes.

    look in documentation for "FileStream Values"

    make a function to do this, quite easy. open the file, read each line, if it is ints you want convert the string to int and bingo.

    good luck!
  • eddybrown
    Options
    Offline / Send Message
    Thanks thats just what I was looking for cheers!
  • eddybrown
    Options
    Offline / Send Message
    Thanks for your help earlier, I've got this so far:
    fn getNumbers =
    (
        local listArray = #()
        
        fileS = openFile "C:\\numbers.txt" mode:"r"
        
        while not eof fileS do
        (
            lineInfo = readLine fileS
            append listArray lineInfo
        )
        
        close fileS
        
        for i in listArray.count do
            (
                print i
            )
    )
    
    
    My only problem is that my last for loop doesn't work. If I print listArray just after close fileS it returns the numbers in the text file. With the last for loop in I get errors in my script:
    -- Error occurred in getNumbers(); filename: C:\Program Files (x86)\Autodesk\3ds Max 2008\Scripts\generator.ms; position: 263
    --  Frame:
    --   lineInfo: "3333333333"
    --   listArray: #("1111009921", "9999001119", "3333333333")
    --   files: <File:C:\numbers.txt>
    -- No "map" function for 3
    
  • r_fletch_r
    Options
    Offline / Send Message
    r_fletch_r polycounter lvl 9
    for i in listarray.count


    should be
    for i in listarray


    or you could do it this way

    for i = 1 to listarray.count do
    (
    print listArray
    )


    "for x in y do" only works on arrays, your giving it an integer (listarray.count). for large data sets use the second method as its much faster.
  • eddybrown
    Options
    Offline / Send Message
    r_fletch_r wrote: »
    should be




    or you could do it this way



    "for x in y do" only works on arrays, your giving it an integer (listarray.count). for large data sets use the second method as its much faster.

    Thanks for explaining that. The 2nd method you suggest works brilliantly. Thanks. :thumbup:
  • r_fletch_r
    Options
    Offline / Send Message
    r_fletch_r polycounter lvl 9
    very welcome :) good luck with the script.
Sign In or Register to comment.