How would I parse this text in LiveCode?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

How would I parse this text in LiveCode?

Post by karmacomposer »

Say I had this text string all over a database table field: &mc_gross_4=9.00&

The field title mc_gross is constant and the & before and after is constant, but the number 1, 2, 3 and so on holds an amount (in this case, 9.00). I need to parse the amount (9.00) for each &mc_gross_xx where xx is a number.

Thanks for your help.

Mike
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: How would I parse this text in LiveCode?

Post by dunbarx »

Hmmm. What would Klaus say?

Oh, yeah. "The itemDelimiter is your friend".

The trick, if you can rely on it, it to find a character or characters that are consistent in the string. In this case the "=" seems to be the one. If you set the itemDelimiter to that char, then the second item in each string will be your value of interest, along with the trailing "&". But this, too, seems to be consistent, so you can lose it explicitly.

Can you do this? Start with the dictionary for "itemDelimiter", then practice, and only then write back if you get stuck.

Craig Newman
WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Re: How would I parse this text in LiveCode?

Post by WaltBrown »

Or try:

Code: Select all

matchText(tMyStringToSearch,"gross_([0-9]*)=([.0-9]+)",tFirstNum,tSecondNum)
tFirstNum will have the 4 from the first token, and tSecondNum will have the floating point number after the equal sign. I tested this against your input string and got "4" and "9.00"

Walt
Walt Brown
Omnis traductor traditor
Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: How would I parse this text in LiveCode?

Post by Mikey »

If you want to do it "the long way", i.e. not using regular expressions, there are many ways to do it.

If you want to get both the suffix and the value,

Code: Select all

set the itemDelimiter to "="
put item 1 of yourThing into mc_gross_suffix
delete char 1 to 10 of mc_gross_suffix

put item 2 of yourThing into theValue
delete last char of theValue

If you want to just get the value,

Code: Select all

delete char 1 to 10 of yourThing
delete last char of yourThing
Post Reply