## calculating if items are same or different

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

### calculating if items are same or different

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).

dunbarx
VIP Livecode Opensource Backer
Posts: 6465
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: calculating if items are same or different

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

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

### Re: calculating if items are same or different

Thanks for the response, but I'm confused as to what this script does.

Can you explain it a bit further?

[-hh]
VIP Livecode Opensource Backer
Posts: 2234
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

### Re: calculating if items are same or different

..........
Last edited by [-hh] on Wed Aug 13, 2014 12:23 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
Posts: 6465
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: calculating if items are same or different

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
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

[-hh]
VIP Livecode Opensource Backer
Posts: 2234
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

### Re: calculating if items are same or different

..........
Last edited by [-hh] on Wed Aug 13, 2014 12:23 pm, edited 1 time in total.
shiftLock happens

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

### Re: calculating if items are same or different

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!!!

[-hh]
VIP Livecode Opensource Backer
Posts: 2234
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

### Re: calculating if items are same or different

..........
Last edited by [-hh] on Wed Aug 13, 2014 12:22 pm, edited 1 time in total.
shiftLock happens

PoLyGLoT
Posts: 105
Joined: Sat Jan 14, 2012 12:37 am

### Re: calculating if items are same or different

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.

[-hh]
VIP Livecode Opensource Backer
Posts: 2234
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

### Re: calculating if items are same or different

..........
Last edited by [-hh] on Wed Aug 13, 2014 12:22 pm, edited 1 time in total.
shiftLock happens

Simon
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

### Re: calculating if items are same or different

HH,
SWEET!
That is crazy fast!
Great stack as well.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!