populate a string array with a shell oneliner
The following code populates speakers with RHVoice's Russian text-to-speech voices whose names begin with the latter 'a'. Note, without calling bash -c "...", the shell used probably isn't bash, so the bash shortcut of {local,} isn't used to look in both locations for the voice files. Note also, any error output will show up at compile time, so suppressing them with 2>/dev/null may not be that important.
nelua.io Overview examples include type hinting and static checks, which are more appropriate for reusable Lua code, but here there's only one use, that use immediately follows, and that use has its own explicit type.
##[[
local function rhvoices()
local list = aster.InitList{}
local out = io.popen('grep language=Russian /usr/share/RHVoice/voices/*/voice.info /usr/local/share/RHVoice/voices/*/voice.info 2>/dev/null')
for line in out:lines() do
local match = string.match(line, '/voices/(a.-)/voice.info')
if match ~= nil then
table.insert(list, aster.value(match))
end
end
out:close()
return list
end
]]
local speakers: []string = #[rhvoices()]#
require 'iterators'
for i, s in ipairs(speakers) do
print(i, s)
endoutput:
$ nelua shellinit.nelua 0 aleksandr-hq 1 aleksandr 2 anna 3 arina 4 artemiy
And proof that this isn't runtime work:
$ nelua --print-code shellinit.nelua |grep anna
static nlstring_arr5 shellinit_speakers = {.v = {{(uint8_t*)"aleksandr-hq", 12}, {(uint8_t*)"aleksandr", 9}, {(uint8_t*)"anna", 4}, {(uint8_t*)"arina", 5}, {(uint8_t*)"artemiy", 7}}};
lax literal generation
Compare Overview#Preprocessor macros emitting AST notes with the following code that also suffices:
##[[
local function create_sequence(n)
local list = aster.InitList{}
for i=1, n do list[i] = aster.value(i) end
return list
end
]]
local seq: []integer = #[create_sequence(10)]#