"contains" but for matching index?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
"contains" but for matching index?
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
- Location: Palo Alto
Re: "contains" but for matching index?
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 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!
Re: "contains" but for matching index?
Let me give you the Javascript implementation so that maybe it's more clear.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
Code: Select all
function isAmatch(string, substring)
{
for(var i = 0; i < substring.length; i++)
{
if(substring[i] != string[i])
{
return false;
}
}
return true;
}
Re: "contains" but for matching index?
The dictionary entry for the "contains" operator says "See also: ... begins with Operator."
Does that work for you?
-- Dick
Does that work for you?
-- Dick
Re: "contains" but for matching index?
SLICK! Works perfectly thanks man!rkriesel wrote: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?
Hi EssoAir,
Best
Klaus
these resolve to TRUE, too, what am I missing?EssoAir wrote:"xyz" contains "yz" == false
and
"xyz" contains "y" == false
Best
Klaus
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: "contains" but for matching index?
Another, 30 year old way, to do it:
Craig Newman
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
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: "contains" but for matching index?
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.]
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
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: "contains" but for matching index?
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
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?
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]
[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.
Re: "contains" but for matching index?
AHA!
OK, now I also know what the OP meant with "index"!
OK, now I also know what the OP meant with "index"!
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: "contains" but for matching index?
Had to correct my previous post, sorry:
Craig's answers are to be read very slowly, he's a sophisticated author.
Craig's answers are to be read very slowly, he's a sophisticated author.
shiftLock happens
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: "contains" but for matching index?
Sparkout.
Craig
Yep, and I am always forgetting that LC offers new and more streamlined language elements. My old ingrained HC thinking often misses those.isn't that function the same as "begins with"
Craig