# Read a table from an external Lua file and print its content

I am working on a project for creating some flashcard, and I have saved the strings I want to include in the flashcard in an external file called all_text.lua and the data are stored in a linked list in the form list = {next = list, value = {'front of the flash card', 'back of the flashcard'}}and now I would like to create a flashcard for each element of list; wich is, give the command

\begin{flashcard}{front of the flashcard}
back of the flashcard
\end{flashcard}


I've tried with

\begin{luacode}
local l = list
while l do
front = l.value[1]
back = l.value[2]
tex.sprint('\string\\begin{flashcard}{'.. front .. '}')
tex.sprint( back .. '\string\\end{flashcard}')
end
\end{luacode}


Any suggestions?

• Your questions would be much easier to answer if you would provide a MWE. Providing a full document would make testing answer much easier, especially since otherwise everyone has to create his own list for testing. Also what is the actual question? "Any suggestions" seems very unclear, are you asking for a general code review or are you asking about a specific problem? Maybe this would be better suited for StackOverflow, because it looks like a general Lua question. – Marcel Krüger May 24 at 13:58

As I've written in comment, I am not sure what the actual question is, but one problem in the provided code is the endless loop:

The loop variable l is never changed, so the while condition never changes. This causes the loop to output infinite repetitions of the first card. This can be fixed by adding l=l.next at the end of the loop:

\documentclass{article}
\usepackage{luacode}
\newenvironment{flashcard}[1]{\paragraph{Front}#1\paragraph{Back}}{}
\begin{document}
\begin{luacode*}
local list
list = {next = list, value = {"Front of the last card", "Back of the last card"}}
list = {next = list, value = {"Front of the middle card", "Back of the middle card"}}
list = {next = list, value = {"Front of the first card", "Back of the first card"}}

local l = list
while l do
local front, back = table.unpack(l.value)
tex.sprint('\\begin{flashcard}{'.. front .. '}')
tex.sprint( back .. '\\end{flashcard}')
l = l.next
end
\end{luacode*}
\end{document}