How to use a match to reference a variable?

Post Reply
DarkArtist
Posts: 21
Joined: Sun Dec 15, 2013 12:36 am

How to use a match to reference a variable?

Post by DarkArtist »

Lets say that there are several kinds/colors of potions that you want to keep track of.

The potions are acquired off the corpses of victims as such: You get a green potion from the corpse.

So everytime I get a green potion I want to increment greenCount, red potion redCount, yellow potion yellowCount, etc...

The long way would be something like:
if matches[2]="red" then redCount=redCount+1
elseif matches[2]="green" then greenCount=greenCount+1...

How do I simplify it such that it uses the matched color to refer to the appropriately named variable? i.e. matches[2]Count ?

Thanks!

User avatar
Belgarath
Posts: 232
Joined: Fri Jul 26, 2013 7:19 am
Discord: macjabeth#7149

Re: How to use a match to reference a variable?

Post by Belgarath »

Code: [show] | [select all] lua
(matches[2] .. "Count") = (matches[2] .. "Count") + 1
However, I would suggest putting them all in a table, like so...
Code: [show] | [select all] lua
colour_counts = colour_counts or {}
colour_counts[matches[2]] = colour_counts[matches[2]] + 1

DarkArtist
Posts: 21
Joined: Sun Dec 15, 2013 12:36 am

Re: How to use a match to reference a variable?

Post by DarkArtist »

Thanks again sir :)

Post Reply