"contains" but for matching index?

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
EssoAir
Posts: 52
Joined: Thu Feb 20, 2014 7:53 pm

"contains" but for matching index?

Post by EssoAir » Thu Nov 20, 2014 9:44 am

Quick question:

How can I use "contains" (or any command, actually) to evaluate if a substring is contained within a string AND the indexes match up so that

"xyz" contains "x" == true
and
"xyz" contains "xy" == true
but
"xyz" contains "yz" == false
and
"xyz" contains "y" == false

I could write this in my sleep in a C-like syntax, but yet again Livecode just baffles me.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: "contains" but for matching index?

Post by Simon » Thu Nov 20, 2014 10:02 am

Hi EssoAir,
I think you want "is among" except for
put "y" is among the chars of "xyz"
Not clear how that is different from
put "x" is among the chars of "xyz"

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

EssoAir
Posts: 52
Joined: Thu Feb 20, 2014 7:53 pm

Re: "contains" but for matching index?

Post by EssoAir » Thu Nov 20, 2014 10:41 am

Simon wrote:Hi EssoAir,
I think you want "is among" except for
put "y" is among the chars of "xyz"
Not clear how that is different from
put "x" is among the chars of "xyz"

Simon
Let me give you the Javascript implementation so that maybe it's more clear.

Code: Select all

function isAmatch(string, substring)
{
     for(var i = 0; i < substring.length; i++)
     {
          if(substring[i] != string[i])
          {
               return false;
          }
     }
     return true;
}
Although, that has a pretty bad BigO complexity, it gets the job done. I'm wondering what the equivalent is for Livecode.

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: "contains" but for matching index?

Post by rkriesel » Thu Nov 20, 2014 11:13 am

The dictionary entry for the "contains" operator says "See also: ... begins with Operator."

Does that work for you?

-- Dick

EssoAir
Posts: 52
Joined: Thu Feb 20, 2014 7:53 pm

Re: "contains" but for matching index?

Post by EssoAir » Thu Nov 20, 2014 12:47 pm

rkriesel wrote:The dictionary entry for the "contains" operator says "See also: ... begins with Operator."

Does that work for you?

-- Dick
SLICK! Works perfectly thanks man!

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: "contains" but for matching index?

Post by Klaus » Thu Nov 20, 2014 2:26 pm

Hi EssoAir,
EssoAir wrote:"xyz" contains "yz" == false
and
"xyz" contains "y" == false
these resolve to TRUE, too, what am I missing?


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: "contains" but for matching index?

Post by dunbarx » Thu Nov 20, 2014 3:59 pm

Another, 30 year old way, to do it:

Code: Select all

on mouseUp
   put "xyz" into tString
   put "xy" into stringToCheck
   answer checkSubString (tString,stringToCheck)
end mouseUp

function checkSubString tString,stringToCheck
   if stringToCheck is in char 1 to the length of stringToCheck of tString then return "Yup, its in there, and at the beginning"
   else return "No Dice!"
end checkSubString
Craig Newman

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: "contains" but for matching index?

Post by [-hh] » Thu Nov 20, 2014 7:10 pm

Simon, Klaus and also me took this wrong at first read.
What EssoAir writes is his first post are his WISHES, NOT the results he saw.

Dick understood the question at once (at least after the long javascript story) and also Craig:
EssoAir meant "begins with", that is

if offset (str,"xyz") = 1 then ...
... with str among the items of "x,xy,yz,y".

Beginning from 7.0 this could be also, depending on the string, instead of "offset":

(A) byteOffset (= offset)
(B) codePointOffset
(C) codeUnitOffset

What is interesting with that is the meaning of "begins with, is in, ends with, contains" in LC 7 with all the new offsets?

If I remember right my testing when 7 came out, then it's still the old one, using "Bytes":

"b begins with a" works in LC 7 still like old "offset(a,b)=1" (Craig's old-as-stone-hedge-function)
"b ends with a" works in LC 7 still like old "offset(a,b)=length(b)-length(a)",
"a is in b" works in LC 7 still like old "offset(a,b)>0" ,
"b contains a" works in LC 7 still like old "offset(a,b)>0".

If this is correct then one could change Craig's substring function, less wordy :-), to different ones that use instead of "offset" one of the 'new-offsets' A, B or C above.

Perhaps a unicode-and-engine specialist can clarify this?
Would be also worth additions to the docs.

[Edit. Corrected, Craigs' function is equivalent to "begins with" (offset=1). Sorry Craig, you are more awake than I do. SparkOut, your question (see below) pushed me to the point.]
Last edited by [-hh] on Thu Nov 20, 2014 9:38 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: "contains" but for matching index?

Post by dunbarx » Thu Nov 20, 2014 7:50 pm

EssoAir.

I am not understanding. In other words, when Klaus wondered why "xyz" contains "y" == false was bogus, it was because he did not consider as well that the placement of "y" was not at the beginning of "xyz".

Is it not the case that in order to return "true", the string "xy" must not only be in "xyz", it must also be the first two chars in "xyz"? The function I wrote does both.

Anyway, even I could write a regex that did all that in one line, which is another option. But is there more to it than just those two criteria?

Craig

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: "contains" but for matching index?

Post by SparkOut » Thu Nov 20, 2014 8:05 pm

er Craig... isn't that function the same as "begins with"? Which is, as I understand, the OP's requirement - the chars of the string to find must match the index position of the string to test, which must needs begin at 1 - unless we start to get involved with padding the start of the string to test with wildcards.

[edit]Oh wait, 30 year old way? I guess that predates "begins with" then![/edit]
Last edited by SparkOut on Thu Nov 20, 2014 9:19 pm, edited 1 time in total.

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: "contains" but for matching index?

Post by Klaus » Thu Nov 20, 2014 9:05 pm

AHA! :D

OK, now I also know what the OP meant with "index"!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: "contains" but for matching index?

Post by [-hh] » Thu Nov 20, 2014 9:41 pm

Had to correct my previous post, sorry:
Craig's answers are to be read very slowly, he's a sophisticated author.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9670
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: "contains" but for matching index?

Post by dunbarx » Sat Nov 22, 2014 5:02 pm

Sparkout.
isn't that function the same as "begins with"
Yep, and I am always forgetting that LC offers new and more streamlined language elements. My old ingrained HC thinking often misses those.

Craig

Post Reply

Return to “Talking LiveCode”