Page 1 of 1

How does or should "Is Among" work ?

Posted: Wed Feb 01, 2017 1:16 pm
by Simon Knight
Hi I am confused by the use of "item" and "items". Specifically I have a field named ColourCodes that holds the following data, these are todo priorities at the beginning of the line followed by a comma and then a named colour :

Normal,LightPink2
Priority,MediumSeaGreen
Note,LightGreen
High,LightGray
Completed,LightSalmon
Hold,LightPink2

I have a handler that wishes to add new priorities to my list with a default colour of "black". First it has to check that the priority does not already have an entry. Using "Hold" as an example I have tried various constructs using "is among" and its various "chunk types" e.g.

Code: Select all

on mouseUp
   set the itemdel to comma
   If "Hold" is among the items of Field "ColourCodes" then
      answer "Found in field"
   else
      answer "Not found in field"
   end if 
   
   put Field "ColourCodes" into tMystring
   If "Hold" is among the items of tMyString then
      answer "Found in string"
   else
      answer "Not found in string"
   end if 
   
   replace CR with " <NL> " in tMyString
   If "Hold" is among the items of tMyString then
      answer "Found in modified string"
   else
      answer "Not found in modified string"
   end if 
end mouseUp  
It appears that the "is among" just does not handle the comma in the rows of data which seems a little odd given that reading each line and then addressing "item 1" of line works as expected.

I have attached a small stack file and would appreciate any comments as to the logic of "is among"

best wishes

Simon K

Re: How does or should "Is Among" work ?

Posted: Wed Feb 01, 2017 1:29 pm
by Klaus
Hi Simon,

everything is working as it should! :D

Lets take a look at your field:
Normal,LightPink2
Priority,MediumSeaGreen
Note,LightGreen
High,LightGray
Completed,LightSalmon
Hold,LightPink2


If you split this into "items" (comma separated) this is the result, which may not be what you exspected:
Item 1 -> Normal
item 2 -> (LightPink2 & CR & Priority)
item 3 -> (MediumSeaGreen & CR & Note)
etc.
Get the picture?
Everything between two commas is ONE item, since you did not specify a LINE but the whole field,
that's why this will work when looping through the LINES of that field!

So in your case I would use "lineoffset" like:

Code: Select all

...
put "Hold" & COMMA into item2find
If lineoffset(item2find,Field "ColourCodes") <> 0 then
      answer "Found in field"
else
      answer "Not found in field"
end if 
...
Best

Klaus

Re: How does or should "Is Among" work ?

Posted: Wed Feb 01, 2017 3:46 pm
by bn
Hi Simon,

with regard to word in your stack

Field "chipmunk":
My friend fred has hairy balls, mixture fuel flaps hood harness brakes

Code: Select all

 if "balls" is among the words of  field "chipmunk" 
it does not work because of the comma, which is part of the word for Livecode if you use "word" -> dictionary "word"

as of LC 7 you have the "trueWord" which is what we usually understand when speaking of word.

Code: Select all

 if "balls" is among the trueWords of  field "chipmunk" then
that finds "balls" in field "chipmunk"

KInd regards
Bernd

Re: How does or should "Is Among" work ?

Posted: Thu Feb 02, 2017 6:08 am
by Simon Knight
Hi Klaus and Bernd,

Many thanks for your comments which have helped in my understanding. I had incorrectly thought that line breaks would be also treated as word or item breaks and I had not realised that LC only uses spaces to delimit words in the command "is among the words".

The "trueWords" keyword is new to me. You have to love the dictionary entry
A trueWord is a word chunk, delimited by Unicode word breaks, as determined by the ICU Library.
The ICU Library is a document of only 450 pages :
Word boundary locations are found according to these general principles:
• Words themselves are kept together
• Numbers are kept together, including any commas, points or currency symbols.
• Apostrophes or hyphens within a word are kept with the word. They are not broken out separately like most other punctuation
• Punctuation, spaces and other characters that are not part of a word or number, are broken out separately, with a boundary before and after each character.
I think I shall adopt a "test before use" rule with both these commands.

Best wishes
SimonK

Oh - just in case you were wondering the phrase starting "My friend...." is a menmonic, learned back in the 1980's, for the checks to be conducted when flying downwind in a DH Chipmunk.