Sorting Arrays numericly

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Pincer
Posts: 3
Joined: Thu Oct 25, 2012 1:59 pm

Sorting Arrays numericly

Post by Pincer » Thu Oct 25, 2012 2:27 pm

OK after much google searching and testing ideas I still can't fix my problem, hoping someone here can help.

I am trying to create a lottery program for my advanced higher computing projecting livecode, it generates 6 random numbers from 1 to 49 then gives the user 2 chocies to choose his own numbers or have the computer generate numbers for him. I have it all well and working apart from 2 problems. Firstly it has repeating numbers sometimes, I have tried using another array of possible numbers it choosing a random number from that then deleteing that number the code for it is as follows

Code: Select all

 on Generate_users_numbers
      local usersnumbers, possiblenumbers, numberoflines, arrayfile, arraypossiblenumbers
   put url ("file:" & "C:/Users/Pincer/Desktop/Computing/Project/a.txt") into possiblenumbers
   put the number of lines in possiblenumbers into numberoflines
   repeat with loop = 1 to numberoflines
      put line loop of possiblenumbers into arrayfile[loop]
   end repeat
   repeat with loop = 1 to 7
      put random(49) into arraypossiblenumbers
      put arrayfile[arraypossiblenumbers] into usersnumbers
      repeat until usersnumbers is not empty then
       put random(49) into arraypossiblenumbers
      put arrayfile[arraypossiblenumbers] into usersnumbers
      end repeat
      put usersnumbers into array_users_numbers[loop]
      delete variable arrayfile[arrayposiblenumbers]
   end repeat
end Generate_users_numbers
The second problem I have is it won't sort either of the arrays with the numbers properly, the closest i could get was a piece of code I got off a website that does sort it but sorts on only the first number, e.g. if I enter say 2,37,8,9,12,24 it should come up with 2,8,9,12,24,37 but instead it comes up with 12,2,24,37,8,9.

Please can someone help me.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Sorting Arrays numericly

Post by Thierry » Thu Oct 25, 2012 3:02 pm

Hi,

Arrays are strange beast :)

You can't rely on any order; that's the way it is.

For sorting them, the idea is to get the keys of the array, which gives you a list
then sort this list, and finally do a loop ( repeat ) on the sorted list of keys
and get the value of each key....

Sorry, no time to code this but hope you get the idea

HTH

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Bernard
Posts: 351
Joined: Sat Apr 08, 2006 10:14 pm

Re: Sorting Arrays numericly

Post by Bernard » Fri Oct 26, 2012 1:35 am

I think this solves both of your problems.

Code: Select all

on mouseUp
   local x,r
   repeat while the number of items in x < 7
      put random(49) into r
      if r is among the items of x then 
         next repeat
      else
         put r & comma after x
      end if
   end repeat
   delete char -1 of x
   sort items of x ascending numeric
   put x into msg
end mouseUp

Pincer
Posts: 3
Joined: Thu Oct 25, 2012 1:59 pm

Re: Sorting Arrays numericly

Post by Pincer » Fri Oct 26, 2012 12:10 pm

Thanks will try it now.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Sorting Arrays numericly

Post by Klaus » Fri Oct 26, 2012 1:41 pm

Another approach, which avoids "repeat until...", that always scares me somehow 8)

This is just like you would do it manually with a sheet of paper and a pencil, think about it :D

Code: Select all

on mouseUp

  ## Write down all number from 1 to 49
  repeat with i = 1 to 49
    put i & CR after tList
  end repeat

  ## Delete last CR
  delete char -1 of tList

  ## Now DRAW six random numbers
  repeat 6
    put random(the num of lines of tList) into tRand
    put line tRand of tList & CR after tRandList

    ## Wipe out the line you just DRAW
    delete line tRand of tList
  end repeat
  delete char -1 of tRandList
  sort tRandlist numeric
  put tRandList into fld 1
end mouseUp
Best

Klaus

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Re: Sorting Arrays numericly

Post by Randy Hengst » Sun Oct 28, 2012 5:23 pm

How about this... similar to what Klauss suggested

on generateLotteryNumbers firstNumber,lastNumber
local tTheNumbers
put empty into tTheNumbers
repeat with theNumber = firstNumber to lastNumber
-- create the list of lottery numbers -- this way there will be no repeated numbers
put theNumber & "," after tTheNumbers
end repeat
delete last char of tTheNumbers -- must remove trailing comma

sort items of tTheNumbers by random(10000)
put item 1 to 6 of tTheNumbers into tTheNumbers
sort numeric items of tTheNumbers ascending
put tTheNumbers into field "LotteryNumbers"
end generateLotteryNumbers
on mouseUp
generateLotteryNumbers 1,49
end mouseUp

be well,
randy

Pincer
Posts: 3
Joined: Thu Oct 25, 2012 1:59 pm

Re: Sorting Arrays numericly

Post by Pincer » Tue Jan 15, 2013 3:05 pm

Sorry I took so long to get back to you guys, been busy with all my subjects and projects. Finaly got my program fully working. In the end went with Bernard's idea. Thanks everyone who posted for the help.

Post Reply