N
N
nevro2019-07-02 19:15:28
Lua
nevro, 2019-07-02 19:15:28

How to loop through an array of strings?

There is an array of strings, you need to access each and read character by character:

lines = {
       "sdss",
       "vfbf"
      }
for i=1,#lines do
   line = lines[i]
   for j=1, #line do print(line[j]) end
end

Questions:
1. Is it possible to make it more elegant in lua?
2. This code prints nil 8 times, although there should be characters. What have I done wrong?
PS did in https://www.lua.org/cgi-bin/demo

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Samsonov, 2019-07-02
@nevro

for i = 1, #lines do
   line = lines[i]
   for j = 1, #line do 
     print(string.sub(line, j, j))
   end
end

https://rextester.com/VKJ93178

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question