Page 1 of 1

linear search

Posted: Tue Jun 09, 2020 4:42 pm
by jpaterson
Having a bit of trouble with my linear search. I have two occurrences of a number in an array. I'd like the output window to show positions of both but it only shows the position of the second number in the array and not the first. I've coded it using a fixed loop (the conditional one only outputs the first and then exits the loop). Any suggestions would be most welcome. Here's my code...

global arrayNumbers
global targetNumber

on mouseUp
initialise
getValue targetNumber
search targetNumber, arrayNumbers
end mouseUp

on initialise
put empty into field output
put 0 into targetNumber
put 45 into arrayNumbers[0]
put 65 into arrayNumbers[1]
put 23 into arrayNumbers[2]
put 67 into arrayNumbers[3]
put 88 into arrayNumbers[4]
put 90 into arrayNumbers[5]
put 1 into arrayNumbers[6]
put 67 into arrayNumbers[7]
put 6 into arrayNumbers[8]
put 22 into arrayNumbers[9]
put 78 into arrayNumbers[10]
put 31 into arrayNumbers[11]
put 99 into arrayNumbers[12]
put 28 into arrayNumbers[13]
put 84 into arrayNumbers[14]
put 54 into arrayNumbers[15]
put 71 into arrayNumbers[16]
put 16 into arrayNumbers[17]
put 49 into arrayNumbers[18]
put 11 into arrayNumbers[19]
end initialise

on getValue @targetNumber
ask"Enter the value you wish to search for"
if the result=cancel then exit to top
put it into targetNumber
put targetNumber into field "output"
end getValue

on search @targetNumber, @arrayNumbers
local found
local counter
put false into found
repeat with counter = 0 to 19
if targetNumber=arrayNumbers[counter] then
put targetNumber & " was found at position " &counter+1 &return into field "output"
put true into found
end if

end repeat
if found=false then
put "Sorry, no matches found" & return into field "output"
end if

end search

Thanks!

Re: linear search

Posted: Tue Jun 09, 2020 4:49 pm
by Xero
Try this:

Code: Select all

global arrayNumbers
global targetNumber

on mouseUp
initialise
getValue targetNumber
search targetNumber, arrayNumbers
end mouseUp

on initialise
put empty into field output
put 0 into targetNumber
put 45 into arrayNumbers[0]
put 65 into arrayNumbers[1]
put 23 into arrayNumbers[2]
put 67 into arrayNumbers[3]
put 88 into arrayNumbers[4]
put 90 into arrayNumbers[5]
put 1 into arrayNumbers[6]
put 67 into arrayNumbers[7]
put 6 into arrayNumbers[8]
put 22 into arrayNumbers[9]
put 78 into arrayNumbers[10]
put 31 into arrayNumbers[11]
put 99 into arrayNumbers[12]
put 28 into arrayNumbers[13]
put 84 into arrayNumbers[14]
put 54 into arrayNumbers[15]
put 71 into arrayNumbers[16]
put 16 into arrayNumbers[17]
put 49 into arrayNumbers[18]
put 11 into arrayNumbers[19]
end initialise

on getValue @targetNumber
ask"Enter the value you wish to search for"
if the result=cancel then exit to top
put it into targetNumber
put targetNumber into field "output"
end getValue

on search @targetNumber, @arrayNumbers
local found
local counter
put false into found
repeat with counter = 0 to 19
if targetNumber=arrayNumbers[counter] then
put targetNumber & " was found at position " &counter+1 &return after field "output"-- "After" not "into". Into will probably replace contents and give you second instance because first was overwritten. You may also need to do return & targetNumber &"was found at" etc. (i.e. do return first not last)... maybe--
put true into found
end if

end repeat
if found=false then
put "Sorry, no matches found" & return into field "output"
end if

end search
See how that goes.
X

Re: linear search

Posted: Tue Jun 09, 2020 4:52 pm
by dunbarx
Hi.

Please use code tags ("</>") for your, er, code. It makes things much easier to read and think about.

Craig

Re: linear search

Posted: Tue Jun 09, 2020 5:06 pm
by dunbarx
This might get you started. In a button script:

Code: Select all

on mouseUp
   put 0 into targetNumber
   put 45 into arrayNumbers[0]
   put 65 into arrayNumbers[1]
   put 23 into arrayNumbers[2]
   put 67 into arrayNumbers[3]
   put 88 into arrayNumbers[4]
   put 90 into arrayNumbers[5]
   put 1 into arrayNumbers[6]
   put 67 into arrayNumbers[7]
   put 6 into arrayNumbers[8]
   put 22 into arrayNumbers[9]
   put 78 into arrayNumbers[10]
   put 31 into arrayNumbers[11]
   put 99 into arrayNumbers[12]
   put 28 into arrayNumbers[13]
   put 84 into arrayNumbers[14]
   put 54 into arrayNumbers[15]
   put 71 into arrayNumbers[16]
   put 16 into arrayNumbers[17]
   put 49 into arrayNumbers[18]
   put 11 into arrayNumbers[19]
   
   repeat for each key tKey in arrayNumbers
      add 1 to dupNumbers[arrayNumbers[tkey]]
   end repeat
   
   repeat for each key dupKey in dupNumbers
      if dupNumbers[dupKey] = 2 then answer dupKey
   end repeat
end mouseUp
Craig

Re: linear search

Posted: Tue Jun 09, 2020 5:39 pm
by jpaterson
Thanks for this. Works a treat thank-you. Rookie error(!)

Sorry, first time poster, I'll remember this for the next time :oops:

Re: linear search

Posted: Tue Jun 09, 2020 6:00 pm
by dunbarx
Not a problem. Takes a little getting used to.

As for the array gadget, please let me know if you follow the procedure. It is a little odd in that there is an array "within" an array. You might step through the handler, watching the changing contents of the arrays in the debugger.

Know also, and this is often more comforting (it usually is for me), that you can transform an array variable into an "ordinary" variable using the "combine" command. Once this is done, all of the power of LC is available to that dataSet. I often find it easier in that form. Once you are finished fooling around, the data can be reconstituted into an array using the "split" command.

In your example I was on the cusp of doing so, but decided to stay in the array world.

Craig

Re: linear search

Posted: Tue Jun 09, 2020 8:38 pm
by AxWald
Hi,

another approach. A mouseUp & a function:

Code: Select all

on mouseUp
   put 45 into myArr[0]
   --  snip - here be the remaining entries :)
   put 11 into myArr[19]
   
   ask "Search what?"
   if it is empty then
      exit mouseUp
   else
      put it into myNum
   end if
   
   put searchArr(myNum,myArr) into myVar                --  see function below
   
   if myVar is empty then
      answer myNum && "doesn't exist in your array."
   else
      answer "Found" && myNum && "in these keys of your array:" && myVar
   end if
end mouseUp

function searchArr what, theArr
   repeat for each line K in the keys of theArr         --  just looping through the array keys ...
      if theArr[K] = what then put K & comma after myVar
   end repeat
   delete char -1 of myVar
   return myVar
end searchArr
Have fun!