Math and commas

Post Reply
Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Math and commas

Post by Aquifn »

So, I want to take this line:

You possess 24 items and are carrying 1,034 gold sovereigns.

and pull out the amount of gold I'm holding into a variable that I can divide.

I currently use:
Code: [show] | [select all] lua
You possess (\d+) items and are carrying ((\d+),(\d+)|(\d+)) gold sovereigns.
and have it do this:
Code: [show] | [select all] lua
goldheld = matches[3]/2

This works for anything under 1k gold. (before the comma is added). Obviously my first part isn't working, it was just me messing around and doing guess work. I want to be able to grab both less than a thousand, and over a thousand, and have it work for the math. I'm not above using more variables.


Thanks!
Last edited by Aquifn on Sat Jun 29, 2013 9:42 am, edited 1 time in total.

User avatar
kevutian
Posts: 217
Joined: Fri Aug 20, 2010 8:18 pm
Location: United Kingdom
Contact:

Re: Math and commas

Post by kevutian »

Code: [show] | [select all] lua
goldheld = matches[2]:gsub(",", "")

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Re: Math and commas

Post by Aquifn »

Should this work?
Code: [show] | [select all] lua
goldheld = (matches[3]/2):gsub(",", "")

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Math and commas

Post by Jor'Mox »

First, you want your pattern to be this:
You possess (\d+) items and are carrying (([\d,]+)) gold sovereigns.
Then you would do this for the math.
goldheld = matches[2]:gsub(",","") / 2

Aquifn
Posts: 19
Joined: Fri Mar 15, 2013 8:37 pm

Re: Math and commas

Post by Aquifn »

Thanks fellas

Post Reply