Page 1 of 1

"contains" but for matching index?

Posted: Thu Nov 20, 2014 9:44 am
by EssoAir
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.

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 10:02 am
by Simon
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

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 10:41 am
by EssoAir
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.

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 11:13 am
by rkriesel
The dictionary entry for the "contains" operator says "See also: ... begins with Operator."

Does that work for you?

-- Dick

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 12:47 pm
by EssoAir
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!

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 2:26 pm
by Klaus
Hi EssoAir,
EssoAir wrote:"xyz" contains "yz" == false
and
"xyz" contains "y" == false
these resolve to TRUE, too, what am I missing?


Best

Klaus

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 3:59 pm
by dunbarx
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

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 7:10 pm
by [-hh]
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.]

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 7:50 pm
by dunbarx
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

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 8:05 pm
by SparkOut
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]

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 9:05 pm
by Klaus
AHA! :D

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

Re: "contains" but for matching index?

Posted: Thu Nov 20, 2014 9:41 pm
by [-hh]
Had to correct my previous post, sorry:
Craig's answers are to be read very slowly, he's a sophisticated author.

Re: "contains" but for matching index?

Posted: Sat Nov 22, 2014 5:02 pm
by dunbarx
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