Pattern matching

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Pattern matching

Post by KayleMaster »

Here I am again.
I'm kinda new to lua's strings, being a C guy and all, and I don't really know every function, so can you help me?

I have this string: wood|1000
so the pattern is word|digits
First I need to get word and compare it, if it's =="wood" for example and then I need to get the value. How would I do that? I could only get either the word with "%a+" or the value "%d+" but I failed getting both. Maybe because I used gsub, dunno.

EDIT: Ok so I got this now:

Code: Select all

local arg = string.match(string_recv, "%a+") 
if arg == "wood" then wood = string.match(string_recv, "%d+") end
but what if I have wood|1000|stone|2000, how would I handle that?
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Pattern matching

Post by Ref »

I'm not too good with pattern matching but what's wrong with:

Code: Select all

str = 'wood|1000'
sep = string.find( str, '|' )
word, value = string.sub( str, 1, sep-1 ), str:sub( sep+1, #str )
or maybe

Code: Select all

word, value = string.match(str, "(%a+)|(%d+)")
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Pattern matching

Post by zorg »

KayleMaster wrote: Wed Apr 19, 2017 5:38 pm but what if I have wood|1000|stone|2000, how would I handle that?
I'd use a different separator for each pair of "key":"value" pairs, makes things simpler. Probably.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Pattern matching

Post by raidho36 »

The gsub function makes a substitution. The match function can retrieve matching substrings.

Code: Select all

t, c = string.match(str, '(%a+)\|(%d+)')
What you looking for is regex tutorial, and user manual. Regular expressions are the to go method of text processing. It replaces manually scanning the string for characters using complicated sets of conditions and switches which gets unwieldy very quickly.
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: Pattern matching

Post by KayleMaster »

I've always wondered how a function can return two values, in this case, to t and c. And I always forget I can do that...
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Pattern matching

Post by raidho36 »

Lua pushes return values to stack, function call arguments too. So there is no problem accepting and returning any number of values. Unused values are simply dropped, whatever's missing is reported as nil.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 228 guests