linear search

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jpaterson
Posts: 2
Joined: Tue Jun 09, 2020 4:36 pm

linear search

Post by jpaterson » Tue Jun 09, 2020 4:42 pm

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!

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: linear search

Post by Xero » Tue Jun 09, 2020 4:49 pm

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

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

Re: linear search

Post by dunbarx » Tue Jun 09, 2020 4:52 pm

Hi.

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

Craig

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

Re: linear search

Post by dunbarx » Tue Jun 09, 2020 5:06 pm

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

jpaterson
Posts: 2
Joined: Tue Jun 09, 2020 4:36 pm

Re: linear search

Post by jpaterson » Tue Jun 09, 2020 5:39 pm

Thanks for this. Works a treat thank-you. Rookie error(!)

Sorry, first time poster, I'll remember this for the next time :oops:
Last edited by jpaterson on Tue Jun 09, 2020 8:15 pm, edited 1 time in total.

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

Re: linear search

Post by dunbarx » Tue Jun 09, 2020 6:00 pm

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

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: linear search

Post by AxWald » Tue Jun 09, 2020 8:38 pm

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!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”