I am a Lua novice, and I would like to know if there is any way for an iterator to filter out or skip certain items in a table from a csv file. In particular, I would like for an iterator to be able to take the rows of a table and only operate on rows with certain tags or arguments in a column. Take for example a file file.csv with the following data:

1, not a prime
2, a prime
3, a prime
4, not a prime
5, a prime
6, not a prime
7, a prime
8, not a prime
9, not a prime

I would like the iterator to filter out or skip either the "a prime" entries or the "not a prime" entries.

I have borrowed the following Lua code from another poster which defines functions for interpreting the contents of the file as a table, then printing the table with generic for. This is stored in a separate file, luacode.lua.

function interpretfile()

    local input = io.open('file.csv', 'r')
    primedata = {}

    for line in input:lines() do
        local split = string.explode(line, ",")
        primerow = {}
        primerow.arg1 = split[1]
        primerow.arg2 = split[2]
        table.insert(primedata, primerow)


function printfile()

    for i,p in pairs(primedata) do
       tex.print(string.format("{%s} is{%s} number.\\\\", p.arg1, p.arg2))


Finally, the lua functions are made to be interpretable in LaTeX such that the table is printed into a pdf.




Is there a way to get the code in the lua file to print out only certain rows of the csv file, such as those with common entries for the second argument? Is there a better way to get the result I am trying to achieve besides using lua?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.