Page 1 of 1
calculating if items are same or different
Posted: Wed Aug 07, 2013 4:29 pm
by PoLyGLoT
Hi all,
Here's the scenario - I'm trying to determine whether the same word appears in a set of lines. For instance, in one example, I have these words:
vorocious
verocious
verocious
verocious
I want livecode to give me the following information: 4 trials --> out of those 4, 3 words are the same, 1 word is different.
Any ideas?
p.s. bonus points for someone who could come up with a way for livecode to recognize that the words are *functionally* the same, it's just that one word is misspelled (e.g., perhaps a calculation based on number of letters in the right position).
Re: calculating if items are same or different
Posted: Wed Aug 07, 2013 6:13 pm
by dunbarx
Hi.
Try this in a button somewhere, assuming your data is in a field "f4":
Code: Select all
on mouseUp
get fld "f4"
repeat with y = 1 to the number of lines of it
put line y of it into counterArray[line y of it][y]
end repeat
end mouseUp
The resulting array will have a list of the line numbers for each instance of your word. Either set a breakpoint at "end mouseUp", or combine the array into something you can see.
As for finding small spelling discrepancies, I suppose you can check for a number of things in each line: the length, as you mentioned, whether only one char in a line differed from another line, or whatever. Can you modify the above script to extend to the chars in each line? The method is about identical.
Craig Newman
Re: calculating if items are same or different
Posted: Wed Aug 07, 2013 7:09 pm
by PoLyGLoT
Thanks for the response, but I'm confused as to what this script does.
Can you explain it a bit further?
Re: calculating if items are same or different
Posted: Sun Aug 11, 2013 11:09 pm
by [-hh]
..........
Re: calculating if items are same or different
Posted: Mon Aug 12, 2013 5:37 pm
by dunbarx
Hi.
I have not checked the scripts that hhBUSwU7p mentioned, but likely they are similar to the following:
Code: Select all
on mouseUp
get fld "f4" --where the list is
repeat for each line tLine in it
add 1 to counterArray[tLine]
end repeat
end mouseUp
Again, set a breakpoint at the end of the handler, or combine the array into a normal variable so you can see the results.
This (and perhaps those others) is a good introduction to how arrays are populated, and worth studying.
Craig
Re: calculating if items are same or different
Posted: Mon Aug 12, 2013 11:46 pm
by [-hh]
..........
Re: calculating if items are same or different
Posted: Thu Aug 15, 2013 11:28 pm
by PoLyGLoT
hhBUSwU7p wrote:The remark of dunbarx (Craig) is very valuable:
Using arrays is *much* faster, if the input is big (say 500 or 1000 objects).
It is like a bulldozer against a shovel for removing a heap of earth. Usually, in most cases, the bulldozer is much more faster, for small heaps nevertheless the shovel. Similarly, for small input sizes and depending on the wanted output format, it may be faster to do it manually (sort input, compare first object against the following objects, count frequencies, delete multiple objects and format the output, all in one walk thru the objects).
Using arrays could be even more advantageous, if the combine command had a formatting parameter to format output. Until now, in order to put the frequencies in the first item (as I want it), you have to walk again thru the combined counterArray.
Anyway, I updated my stacks 'countedSets' on revOnline = 'User Samples' (this is newer than Livecode Share, which is currently not updated). Now you will find a check option for using the counterArray method given by dunbarx. The script does a timing, so you can compare and see the advantage of using arrays if input size increases.
You are so helpful, but I feel slightly silly. I cannot find the information you are referencing. Do I search within the livecode program?
I very much want to understand arrays - and I will be working with this more once I have a little more free time. Thank you!!!
Re: calculating if items are same or different
Posted: Fri Aug 16, 2013 12:39 am
by [-hh]
..........
Re: calculating if items are same or different
Posted: Fri Aug 16, 2013 2:57 pm
by PoLyGLoT
Hello again,
I found the page you are referencing! I clicked on your stack, and it said "stack downloaded". However, I cannot find where the stack was downloaded TO. :-/
Sorry for all the questions - hope to hear from you soon. I wish I could give you my email...
Take care.
Re: calculating if items are same or different
Posted: Fri Aug 16, 2013 6:37 pm
by [-hh]
..........
Re: calculating if items are same or different
Posted: Fri Aug 16, 2013 10:28 pm
by Simon
HH,
SWEET!
That is crazy fast!
Great stack as well.
Simon