Crafted
|
  |
| Joined: 13 Jul 2009 |
| Total Posts: 740 |
|
|
| 18 Sep 2015 06:06 PM |
if I have tile[1][2] how do I get 1 in an x variable and 2 in a y variable?
pattern = "%d" will give me 1, but what if I want to get the 2? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Sep 2015 06:08 PM |
blah = "[123][456]"; local x, y = blah:match("%[(%d+)%]%[(%d+)%]");
Although if there might be non-whole numbers, it might be better to match everything in the brackets. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 18 Sep 2015 06:08 PM |
for thing in string.gmatch(s,"%d") do --blah end |
|
|
| Report Abuse |
|
|
Crafted
|
  |
| Joined: 13 Jul 2009 |
| Total Posts: 740 |
|
|
| 18 Sep 2015 06:13 PM |
cntkillme's method worked perfectly, though I'm not exactly sure why.
the string patterns are so confusing. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 18 Sep 2015 06:13 PM |
he hacks
while true do ur mom |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Sep 2015 06:15 PM |
I'll explain it as well as I can without getting into too much detail
blah = "[123][456]"; local x, y = blah:match("%[(%d+)%]%[(%d+)%]");
Because '[' and ']' are special, we need to 'escape' them (treat them as a regular character we want to look for) so doing %[ will escape it (and %] as well).
Anything in parenthesis (called captures) are returned, and since I have two captures, 2 values are returned (x and y, respectively).
So in the brackets, I search for 1 or more digits, and put them around parenthesis. I do that twice to get the x and y.
In short, I capture a sequence of digits )twice) in brackets and return them. |
|
|
| Report Abuse |
|
|
Crafted
|
  |
| Joined: 13 Jul 2009 |
| Total Posts: 740 |
|
|
| 18 Sep 2015 06:22 PM |
I see what your saying.
One more question:
Why do (%d+) instead of (%d)? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Sep 2015 06:24 PM |
| %d will only match a single digit, I want to match 1 or more. |
|
|
| Report Abuse |
|
|
Crafted
|
  |
| Joined: 13 Jul 2009 |
| Total Posts: 740 |
|
|
| 18 Sep 2015 06:26 PM |
| But in a case were the only possible values of each are a single digit (1-9) it would be find just to use %d? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Sep 2015 06:27 PM |
Yeah, honestly you can actually just do this if that's the case:
x, y = match("(%d).-(%d)") |
|
|
| Report Abuse |
|
|
Crafted
|
  |
| Joined: 13 Jul 2009 |
| Total Posts: 740 |
|
|
| 18 Sep 2015 06:28 PM |
| Alright, thanks for the help!! |
|
|
| Report Abuse |
|
|