简体   繁体   中英

Lua - How to ignore a result from a table iteration without removing it?

I wanto to create a crossword puzzles's solver with Lua. I'm not used to this language tho and my english is poor, sorry for that.

I have to iterate multiples times the same table of tables checking if the given word is present or not and, if present, replace every char of that word in the table with a "*" simbol.

For example:

schema= {
{"A","B","C","D","H","F","G","W","T","Y"},
{"U","H","E","L","L","O","I","I","O","L"},
{"G","F","D","R","Y","T","R","G","R","R"}}


function(schema,"HELLO")
schema= {
{"A","B","C","D","H","F","G","W","T","Y"},
{"U","*","*","*","*","*","I","I","O","L"},
{"G","F","D","R","Y","T","R","G","R","R"}}

For now i'm focusing on find the word scanning the table from left to right. Here's my code:

i = 1
t = {}

for k,w in pairs(schema) do
    t[k] = w
end

cercaPrima = function(tabella,stringa)
    for v = 1, 10 do
        if string.sub(stringa,1,1) == t[i][v] then
            print(t[i][v]) v = v+1
            return cercaDS(t,stringa,i,v)
        else
            v = v+1
        end
    end
    if i < #t then
        i = i+1
        cercaPrima(tabella,stringa)
    else
        return print("?")
    end
end

cercaDS = function(tabella,stringa,d,s)
    local o = 2
    local l = 2
    while o <= #stringa do
        if string.sub(stringa,o,l) == tabella[d][s] then
            print(tabella[d][s])
            tabella[d][s] = "*"
            s=s+1
            o=o+1
            l=l+1
        else
            l=l-1
            s=s-l
            o=#stringa+1
            tabella[d][s] = "*"
            return cercaPrima(tabella,stringa)
        end
    end
end

cercaPrima(schema,"HELLO")

It's probably overcomplicated, but my question is: How can I make it ignore the first "H" (not turning it into a "*") while keep iterating the table looking for another "H" who fits the criteria?

My goal is to create a function who takes a table and a list of words in input, iterates the table looking for every single word, if it finds them all it replaces every char of every word found in the table with a "*" and print the remaining characters as a string.

Another problem that i'll probabily have is: what if a char of a word is a char of another word too? It will read "*" instead of the real char if it has already found the first word.

Should I create a new table for every word I'm looking for? But then how can i merge those table togheter to extrapolate the remaining characters?

Thank you for your help!

If you want to ignore something one time you can use a conditional statement. Just remember that you encountered it already using a variable. But I don't see how this makes sense here.

A problem like this is probably solved better by turing each line and column into strings and then stimply search the strings for words.

I find string.gsub() is a great find and replacement tool.
Maybe it hit not all requirements but maybe it inspire you.

> function cercaPrisma(tab,txt) for i=1,#tab do print((table.concat(tab[i]):gsub(txt, ('*'):rep(txt:len())))) end end
> cercaPrisma(schema, 'HELLO')
ABCDHFGWTY
U*****IIOL
GFDRYTRGRR
> cercaPrisma(schema, 'DRY')
ABCDHFGWTY
UHELLOIIOL
GF***TRGRR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM