I want to replace abbreviated text with the full text according to a lookup table. There can be more than one abbreviation for the same entry.
For example:
local lookup = {
['Harry Potter and the Philosopher\'s Stone'] = { 'HP1', 'PS', 'SS' },
['Harry Potter and the Chamber of Secrets'] = { 'HP2', 'CoS' },
}
How to traverse a table like this? Is this a 3D table, or is it something else? Is this table even the right tool to use for this job?
Could it be something like this what you are looking for?
<script src="//github.com/fengari-lua/fengari-web/releases/download/v0.1.4/fengari-web.js"></script>
<script type="application/lua">
local abbrTitle = "PS"
local fullTitle = ""
local lookup = {
['Harry Potter and the Philosopher\'s Stone'] = { 'HP1', 'PS', 'SS' },
['Harry Potter and the Chamber of Secrets'] = { 'HP2', 'CoS' },
}
for k, v in pairs(lookup) do
if type(v) == "table" then
for i = 1, #v do
if v[i] == abbrTitle then
fullTitle = k
break --this is optional
end
end
end
end
print(fullTitle)
</script>
It's only a matter of go traversing the data until you do a match, and there is nothing really special in the way of doing it for the kind of table you are using, which turns to be only a dictionary table containing other array type tables. They have just to be traversed differently, the first one by parsing all its key/values pairs and the others by go passing throw all their numerical indexes.
And well, here I'm simply printing the result but, once caught, you really can do with it whatever you want.