lua question: matches[n] shorthand syntax?

Post Reply
Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

lua question: matches[n] shorthand syntax?

Post by Caled »

Is there any chance that there exists a shorthand syntax for matches[n]?

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: lua question: matches[n] shorthand syntax?

Post by Vadi »

Someone else raised this issue too. Compared to zmud/cmud where it's just %n, this is too bulky.

How about m[n]?

Caled
Posts: 403
Joined: Thu Apr 09, 2009 4:45 am

Re: lua question: matches[n] shorthand syntax?

Post by Caled »

That would work for me.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: lua question: matches[n] shorthand syntax?

Post by Vadi »

The problem is now adjusting it, two years later. We'd have to keep both matches[2] and m[2]... hm. I wonder if there's a metatable hack you could do to mirror the values.

User avatar
tsuujin
Posts: 695
Joined: Fri Feb 26, 2010 12:59 am
Location: California
Contact:

Re: lua question: matches[n] shorthand syntax?

Post by tsuujin »

Vadi wrote:The problem is now adjusting it, two years later. We'd have to keep both matches[2] and m[2]... hm. I wonder if there's a metatable hack you could do to mirror the values.
Yeah, metatables can certainly be used for this. Here's a bit of code I hacked out which should explain the process fairly well:
Code: [show] | [select all] lua
mtTest = mtTest or {}
mtDouble = mtDouble or {}

setmetatable(mtTest,{
	__newindex = function(self,key,value) rawset(self,key,value) mtDouble[key] = value end
})

mtTest["test1"] = "test"
mtTest["test2"] = "test2"
mtTest["testing"] = "again"
display(mtTest)
display(mtDouble)
rawset accesses table data -without- calling metatable information, without it you get stack overflows on trying to add to a table via it's own __newindex function.

User avatar
Omit
Posts: 190
Joined: Sun Aug 01, 2010 10:54 pm
Location: Middle Earth
Contact:

Re: lua question: matches[n] shorthand syntax?

Post by Omit »

After reading anouther post on the forum. It occured to me that you could always put the following at the head of your trigger scripts... given that 'matches' is a Lua table.
Code: [show] | [select all] lua
m=matches
--any other code here...
(I have not tested but being that m and matches are global tables... you may only have to do this once.)

:!:

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: lua question: matches[n] shorthand syntax?

Post by Denarii »

Omit wrote:After reading anouther post on the forum. It occured to me that you could always put the following at the head of your trigger scripts... given that 'matches' is a Lua table.
Code: [show] | [select all] lua
m=matches
--any other code here...
(I have not tested but being that m and matches are global tables... you may only have to do this once.)

:!:
This doesn't work, I believe it's because the matches table is recreated every time an alias/trigger files, and it breaks the reference.

This works:
Code: [show] | [select all] lua
m = setmetatable({}, { __index = function(_, key) return _G["matches"][key] end })

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: lua question: matches[n] shorthand syntax?

Post by Vadi »

Yeah, it does do that. I wonder if it's the most optimal thing to do now, hm.

Post Reply