Strange results with "if string contains"

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
heyvern
Posts: 30
Joined: Sun Feb 21, 2010 12:38 am

Strange results with "if string contains"

Post by heyvern » Tue Mar 09, 2010 12:13 pm

I have a list of strings separated by returns. In a custom function I loop through each line comparing a string to the lines which contain one word each.
Here is the value in my global variable that is giving me a false true value:

"scale>"

The string that is compared to this value that returns an incorrect "true" match is this:

"scale_compensation true"

If I try this with a direct string comparison it works correctly:

Code: Select all

If "scale_compensation true" contains "scale>" then
  put true into message
else
  put false into message
end if
The above returns false as expected.

But if the same match is done via a loop through a list of words then it returns a TRUE when it hits that "scale_compensation true" string value.
the global variable gTransforms contains:

Code: Select all

--Global gTransforms--
translation>
scale>
rotation_x>
rotation_y>
rotation_z>
flip_h>
flip_v>
shear>
I send the following line of another string:

"scale_compensation true"

to the function below:

Code: Select all

function ReversTransforms theText
  repeat for each line j in gTransforms
    if theText contains j then
       put true into returnVal
       exit repeat
     else
       put false into returnVal
     end if
  end repeat
  return returnVal
end ReversTransforms
For some reason the string containing "scale_compensation true" returns true by matching "scale>"

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Strange results with "if string contains"

Post by bangkok » Tue Mar 09, 2010 12:29 pm

Code: Select all

  repeat for each line j in gTransforms
    if theText contains j then
It should be :

Code: Select all

    if theText contains line j of gTransforms then
no ?

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

Re: Strange results with "if string contains"

Post by bn » Tue Mar 09, 2010 2:01 pm

Heyvern,
your code works for me as it is. Could it be that you have something in your global, trailing line with only a space for example, that returns the erroneous true?
regards
Bern

heyvern
Posts: 30
Joined: Sun Feb 21, 2010 12:38 am

Re: Strange results with "if string contains"

Post by heyvern » Wed Mar 10, 2010 1:11 am

You are a genius! :)

That was it. I did a copy and paste for each line to insert the freaking words into my global variable and forgot to delete the "& return" on the last item!!!! Dang extra return. Odd though. I thought doing "for each line k" wouldn't have a return that matches but apparently it does.

Thanks dude!

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

Re: Strange results with "if string contains"

Post by bn » Wed Mar 10, 2010 11:39 am

hyvern,
heyvern wrote:You are a genius
That is plain wrong, I have tons of evidence to refute that :)

heyvern wrote: That was it. I did a copy and paste for each line to insert the freaking words into my global variable and forgot to delete the "& return" on the last item!!!! Dang extra return. Odd though. I thought doing "for each line k" wouldn't have a return that matches but apparently it does.!
Although a trailing return can do all sort of funny things it does not explain in my testing what happened. A simple trailing return was no problem.
There must have been a space also (or something else) that matched to your string.
regards
Bernd

heyvern
Posts: 30
Joined: Sun Feb 21, 2010 12:38 am

Re: Strange results with "if string contains"

Post by heyvern » Wed Mar 10, 2010 8:30 pm

It could be there was something odd with my string that I "fixed" accidentally, but it was incorrect with the trailing return, and correct when I removed it. The source data is very "precise". No extra spaces, everything indented with tabs. However it is possible there are extra returns. I am at that stage of production where the code is a bit "messy". It all works but it isn't exactly... straight forward. I have started to consolidate and clean things up. I am VERY new with RR. Just started the second month of the demo. Most of my expertise is with lua so there is a bit of a learning curve. I think the hardest part of using RR is dealing with "extra" returns when splitting with delimiters. Splitting items seems to introduce unexpected returns depending on where the delimiter is. This is user error for sure. Sometimes my item delimiters are on their own separate line which causes an extra line.

Post Reply