Question

I'm data-mining information from a website, and one of the things I must do is change a piece of information from what the page gives me into another piece of information, and turn that second piece of information into a variable. I simply cannot figure out a way to do this, however.

Given the table below:

t = {big = "tall", little = "short", fat = "wide", skinny = "thin"}

... how can I make it so when I do:

adj = string.match(page,'Adjective: (%w+)')

... and it matches big, I can return the value of tall but turn it into a table? I have tried using a function, which didn't work, and I'm not wanting to do something like t[adj]={} because I'm not wanting to make a sub-table of t.

I feel like it's staring me right in the face, but I'm drawing a blank.

Edit for clarification:

I know how to get the adjective that I need, or how to show the value for big, etc. What I'm talking about is the ability to do this:

  1. Find the value based on the adjective found. In this case, big was found, the value is tall.
  2. Take the value found (again, in this case, tall) and turn that value into its own table. Then I'd have the table from above, and a new one that hasn't been populated with, called tall. That way, after I populate it with information, I can call it by tall["somekeyvalue"]

Does that make a bit more sense now?

Was it helpful?

Solution

If you are not bothered with global variable, you can use the global environement table.

After getting your value (by t[adj]), you can create your table like this :

_G[t[adj]] = {}

You will be able then to access to the table tall everywhere

OTHER TIPS

It sounds to me that when you match an adjective, you want to look it up in your table and return whatever alias you've got in there.

To do that in lua, you'd use [] to look up the table:

t = {big = "tall", little = "short", fat = "wide", skinny = "thin"}
adj = string.match(page,'Adjective: (%w+)')
print(t[adj])

If it matched "Adjective: big" in page, it will print tall. Is this what you want? The part about "turn it into a table" is confusing to me.

Since I don't know how to create a dynamic variable in Lua I can suggest you to create a table that will be the container of the other tables you create so you can use it by reference like this "result.tall" and this will be a table with the elements you add to it.

Check out this example:

-- you have to initialize it
result = {}
result.tall = {}
result.little = {}

--insert the elements that can be another tables or not
table.insert (result.tall, 'enormous')
table.insert (result.little , {s = 'short'})

-- so you can access it later
for k,v in pairs(result.tall) do
    print(k,v)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top