Lua:从先前设置的变量创建一个表

发布于 2025-01-06 06:29:12 字数 799 浏览 0 评论 0原文

我正在从网站上挖掘数据信息,我必须做的一件事是将页面提供给我的一条信息更改为另一条信息,并将第二条信息转换为变量。然而,我根本想不出一种方法来做到这一点。

给出下表:

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

...当我这样做时,我该如何做到这一点:

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

...并且它与 big 匹配,我可以返回 tall 的值,但将其翻转到一个表?我尝试过使用一个函数,但它不起作用,并且我不想做类似 t[adj]={} 的事情,因为我不想创建一个子表t

我感觉它正盯着我的脸,但我却一片空白。

编辑以澄清:

我知道如何获取我需要的形容词,或者如何显示大的值等。我所说的是执行此操作的能力:

  1. 根据找到的形容词查找值。在本例中,找到了 big,其值为 tall
  2. 获取找到的值(同样,在本例中为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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2025-01-13 06:29:12

如果您不关心全局变量,可以使用全局环境表。

获得您的值(通过 t[adj])后,您可以像这样创建表格:

_G[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 :

_G[t[adj]] = {}

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

亽野灬性zι浪 2025-01-13 06:29:12

在我看来,当您匹配形容词时,您想在表中查找它并返回其中的任何别名。

要在 lua 中执行此操作,您可以使用 [] 查找表格:

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

如果它与 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:

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.

旧伤慢歌 2025-01-13 06:29:12

因为我不知道如何在 Lua 中创建动态变量,所以我建议您创建一个表,该表将成为您创建的其他表的容器,这样您就可以像“result.tall”一样通过引用使用它,这将是一个包含您添加到其中的元素的表格。

看看这个例子:

-- 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文