0

in displaying an array in lua, it always start with 1, so i used it in my sql query if i am using select * from ... as reference of table.id . my problem now is what if the sql query of table.id will not start with 1, or it will be like [3,5,6, ...]?

my code is like this,

local data = {}

for row in db:nrows("SELECT song.id as id, song.title as song, artist.name as artist, genre.name as genre, album.title as album FROM song, artist, genre, album WHERE song.artist_id = artist.id AND song.genre_id = genre.id AND song.album_id = album.id AND song.duration = 120.00") do
                        data[row.id] = {}
                        data[row.id].song = row.song
                        data[row.id].artist = row.artist
                        data[row.id].genre = row.genre
                        data[row.id].album = row.album
end

so the output of row.id is [3,5,6, ..] because i used the row.id as the index of the array data.

my question is what should i do or use so that the index of array data will become like this, [1,2,3,....]?

1 Answer 1

2

You can just use an index variable:

local data = {}
local next = 1

for row in db:nrows("SELECT song.id as id, song.title as song, artist.name as artist, genre.name as genre, album.title as album FROM song, artist, genre, album WHERE song.artist_id = artist.id AND song.genre_id = genre.id AND song.album_id = album.id AND song.duration = 120.00") do
    data[next] = row
    next = next + 1
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.