Lua:从先前设置的变量创建一个表
我正在从网站上挖掘数据信息,我必须做的一件事是将页面提供给我的一条信息更改为另一条信息,并将第二条信息转换为变量。然而,我根本想不出一种方法来做到这一点。
给出下表:
t = {big = "tall", little = "short", fat = "wide", skinny = "thin"}
...当我这样做时,我该如何做到这一点:
adj = string.match(page,'Adjective: (%w+)')
...并且它与 big
匹配,我可以返回 tall
的值,但将其翻转到一个表?我尝试过使用一个函数,但它不起作用,并且我不想做类似 t[adj]={}
的事情,因为我不想创建一个子表t
。
我感觉它正盯着我的脸,但我却一片空白。
编辑以澄清:
我知道如何获取我需要的形容词,或者如何显示大的值等。我所说的是执行此操作的能力:
- 根据找到的形容词查找值。在本例中,找到了
big
,其值为tall
。 - 获取找到的值(同样,在本例中为
tall
)并将该值转换为自己的表。然后我会得到上面的表格,以及一个尚未填充的新表格,名为tall
。这样,在我用信息填充它之后,我可以通过tall["somekeyvalue"]
调用它,
现在这样更有意义吗?
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:
- Find the value based on the adjective found. In this case,
big
was found, the value istall
. - 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, calledtall
. That way, after I populate it with information, I can call it bytall["somekeyvalue"]
Does that make a bit more sense now?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不关心全局变量,可以使用全局环境表。
获得您的值(通过 t[adj])后,您可以像这样创建表格:
然后您将能够在任何地方访问表格 tall
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 :
You will be able then to access to the table tall everywhere
在我看来,当您匹配形容词时,您想在表中查找它并返回其中的任何别名。
要在 lua 中执行此操作,您可以使用
[]
查找表格:如果它与
page
中的“Adjective: big”匹配,它将打印tall
。这是你想要的吗?关于“把它变成桌子”的部分让我感到困惑。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:If it matched "Adjective: big" in
page
, it will printtall
. Is this what you want? The part about "turn it into a table" is confusing to me.因为我不知道如何在 Lua 中创建动态变量,所以我建议您创建一个表,该表将成为您创建的其他表的容器,这样您就可以像“result.tall”一样通过引用使用它,这将是一个包含您添加到其中的元素的表格。
看看这个例子:
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: