简体   繁体   中英

generating list/array from a text file in LUA

I'm trying to handle a text file containing a list of numbers (generated from a python script) into a LUA script.

the text file contain this (I formatted it in the python script to match the list syntax of LUA) :

{"0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0", "834.3", "1667.6", "2500.9", "3334.2", "4167.5", "5000.8", "5834.1", "6667.4", "7500.7", "8334.0", "9167.3", "10000.6", "10833.9", "11667.2", "12500.5", "13333.8", "14167.1", "15000.4", "15833.7", "16667.0", "17500.3", "18333.6", "19166.9", "20000.2", "20833.5", "21666.8", "22500.1", "23333.4", "24166.7", "25000.0", "22244444.44", "44463888.89", "66683333.33", "88902777.78", "111122222.22", "133341666.67", "155561111.11", "177780555.56", "200000000.0", }

However when I try to read it in LUA, first I use this command (where "params.style_weight_list" is a link to the text file previously generated) :

print("SW LIST = ",params.style_weight_list)

which gives me this result :

SW LIST =   {"0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0", "834.3", "1667.6", "2500.9", "3334.2", "4167.5", "5000.8", "5834.1", "6667.4", "7500.7", "8334.0", "9167.3", "10000.6", "10833.9", "11667.2", "12500.5", "13333.8", "14167.1", "15000.4", "15833.7", "16667.0", "17500.3", "18333.6", "19166.9", "20000.2", "20833.5", "21666.8", "22500.1", "23333.4", "24166.7", "25000.0", "22244444.44", "44463888.89", "66683333.33", "88902777.78", "111122222.22", "133341666.67", "155561111.11", "177780555.56", "200000000.0", }    

yet everything seems good (there is several blank spaces before the first bracket, don't know why though)

but when I try to access a specific item from the list this way (frameIdx is an int) :

local neu_sw = params.style_weight_list[frameIdx]

it return a nil value, so I think I got to convert the imported list from a string to a proper usable list in LUA but don't know how ? anybody has an idea ?

When you're unsure what's happening in the script you can always debug the code.

  1. Use print and type to see what value you have and what its type is.

  2. Inspect your table with for k,v in pairs(tbl) do print(k,v) end to see if it really has that key.

As I can see, params.style_weight_list is not a table but a string. You can use load to convert string into script with a Lua table, loadfile to read it from a file directly (or with require , this requires you to prepend return at the beginning of the file), or make a parser that reads values (more proper way but complicated for beginners)

Thanks for your help Nifim and Spar !

thanks to your insight I got it working. if this can help, here what I did :

  • export my python array into a txt file ( with a lua extension, probably useless but helped me keeping it clear in my folder) with one value by line and no other formating elements. "a" is my array in python.

    LUA_in = n*'{}\\n'
    LUA_format = LUA_in.format(*a)
    LUA_file = open("sw.lua","w")
    LUA_file.write(LUA_format)

  • and then read it in lua, create an empty table, add one value/line per table lines and read the specific line I need.

    sw_array = {}
    for line in io.lines("sw.lua") do
    table.insert(sw_array, line)
    end

    local cur_sw = sw_array[frameIdx+1]

where frameIdx is the specific value Index I want to read (and "+1" because lua tables start counting from 1 and python from 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM