matchChunk Difficulty

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

matchChunk Difficulty

Post by WaltBrown » Tue May 19, 2009 9:34 pm

I've tried a zillion different ways, but I cannot get this to work:

Code: Select all

put empty into findList
if matchChunk(textBuffer,testValue,findList)=TRUE then
         combine findList with return
         answer findList with "OK"
end if
The answer triggers but I cannot get any visible contents from the variable "findList". matchChunk comes back TRUE (I am using known test data).

Any pointers?
Thanks, Walt
Walt Brown
Omnis traductor traditor

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Tue May 19, 2009 11:12 pm

Walt,

looking into this I also tried many times, even read the documentation... finally resorted to the plugin RegEx Builder from Frederic Rinaldi that comes with Rev. Played around with it and found this syntax which it lets you copy

In this example I try to find the string "and" in a bunch of characters.

Code: Select all

   put field 1 into textBuffer
   put  "(?i)(and)" into testValue
   put empty into s1
   put empty into e1
   get MatchChunk(textBuffer,testValue,s1,e1)
   if it is true then
      answer "Start of string" && s1 && " End of string" && e1 with "OK"
   else 
      answer "not found"
   end if
so apparently you have to use the get form to fill in the variables and it has true or false. Your variant returns true or false, but fails to fill in the start and end position.
btw, combine applies to arrays, matchChunk does not return an array, it just puts the start position and the end position into the variables.
Apparently if you build a more complicated RegEx then you can predefine variables that will contain the other options. (I did not try that, its according to the docs)
I am afraid if you are looking for a list of all the occurences of a search pattern then I dont see how matchChunk returns this, its always the first occurence. At least in my little experimentation on this.


Well Rinaldi to the rescue (you might remember him from the Hypercard days he did all the beautiful externals that made Hypercard overcome many of its limitations)

May be someone more experienced with the matchChunk function can shed more light on this.
regards
Bernd

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

Post by WaltBrown » Tue May 19, 2009 11:41 pm

Thanks, Bernd, that is exactly what I am looking for, a list of match locations in a file.

I'll play around, you gave me excellent information, thanks!
Walt Brown
Omnis traductor traditor

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Wed May 20, 2009 12:14 am

Walt,

somehow I have the impression you are still looking for the same problem that came up in this threat:
http://forums.runrev.com/phpBB2/viewtop ... highlight=
did my suggestion help you any?
It would be an easy way to find all occurences of a string in a text. It actually gives you the occurences as position of characters.

Or you might explain a little more what you have in mind.

regards
Bernd

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

Post by WaltBrown » Wed May 20, 2009 8:07 pm

Bernd,

I pasted your code into my script and yours worked perfectly and mine didn't. So I moved yours down to where mine was, one statement at a time, one variable at a time, everything, and mine still doesn't work. I am only getting 0 for the beginning and ending character locations when matchChunk returns true.

I have another problem in there, I just need to continue changing one thing at a time until I figure it out. There is something funny happening, like one of the loops is making new copies of some of the local variables, changing them, but then the original values are in the variables after exiting the loop.

Is there a global setting for repeat loops using variables by reference or not?

I'll check out the other thread. Thanks
Walt
Walt Brown
Omnis traductor traditor

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

Post by WaltBrown » Wed May 20, 2009 8:10 pm

And yes, it is the same problem I think. I think I don't have complete grasp on some of the find functions like offset or marchChunk yet.

Walt
Walt Brown
Omnis traductor traditor

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Wed May 20, 2009 9:17 pm

Is there a global setting for repeat loops using variables by reference or not
Not that I knew of.

If your script does not work out for you, you might want to consider posting the relevant part. There are people here on the forum who read xtalk before breakfast... And explain...
:)
regards
Bernd

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

Post by WaltBrown » Fri May 22, 2009 12:13 am

I think I got it Bernd, I was not seeing the specific requirements for a "regular expression". I was neglecting the parentheses in the string sent. I had assumed for a single word that was not necessary, but it is.

This returned TRUE but did NOT fill the positionVarsList

Code: Select all

put field mySearchString into tSearchString
I had to have (note the additional parentheses):

Code: Select all

put "("&field mySearchString&")" into tSearchString
get matchChunk(someLargeText, tSearchString, startChar, endChar)
It would still work and return TRUE without the parenthesis in the search string but would not fill the start and end position variables!

And it even says so in the Dictionary, but I didn't "get it".:
If the regularExpression includes a pair of parentheses, the position of the substring matching the part of the regular expression
inside the parentheses is placed in the variables in the positionVarsList.
Sigh,
Walt
Walt Brown
Omnis traductor traditor

Post Reply