array functions (length, sort, push, pop, shuffle)

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

merrillp
Posts: 1
Joined: Thu Feb 01, 2007 10:57 pm

array functions (length, sort, push, pop, shuffle)

Post by merrillp » Mon Feb 26, 2007 7:17 pm

I am interested in whether revolution provides array functions that are
available in other programming languages such as
a function to find the number of elements in an array (length)
a function to push a value onto the end of an array (push)
a function to pop an element from the beginning or end of an array (pop)
a function to sort the elements of an array
a function to shuffle the elements of an array

Thanks

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5145
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by Mark » Mon Feb 26, 2007 11:56 pm

Dear Merrillp,

1) a function to find the number of elements in an array (length)

Code: Select all

put the number of lines of (the keys of myArray)
2) a function to push a value onto the end of an array (push)

Code: Select all

put myArray[last line of the keys of myArray]
(note that arrays do not have a strict order)

3) a function to pop an element from the beginning or end of an array (pop)

not sure what you mean

4) a function to sort the elements of an array

No, because arrays don't have a strict order. You can sort the keys, though:

Code: Select all

put the keys of myArray into myKeys
sort lines of myKeys
repeat for each line myKey in myKeys
  -- do something with myArray[myKey]
end repeat

5) a function to shuffle the elements of an array

Again, can't do this because arrays don't have a strict order, but you can shuffle the keys:

Code: Select all

put the keys of myArray into myKeys
sort lines of myKeys by random(number of lines of myKeys)
repeat for each line myKey in myKeys
  -- do something with myArray[myKey]
end repeat
If this doesn't do what you want, it might be because sorting by a random number doesn't always mean that each line has a different number.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

June
Posts: 2
Joined: Tue May 01, 2012 1:33 am

Re: array functions (length, sort, push, pop, shuffle)

Post by June » Tue May 01, 2012 1:48 am

Hi Mark,

I, too, am interested in the concept of shuffling the keys of an array. Example: I have an array that contains 64 smaller arrays. I want to be able to select each of the smaller arrays in a random order, BUT I want to be sure that each one gets used once and only once. If I could shuffle the keys then I could use something similar to your example (using shuffle rather than random).

------ The script I am refereing to is
put the keys of myArray into myKeys
sort lines of myKeys by random(number of lines of myKeys)
repeat for each line myKey in myKeys
-- do something with myArray[myKey]
end repeat
---------

So - is there any plan to implement a Shuffle function?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5145
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by Mark » Tue May 01, 2012 2:01 am

Hi June,

As I showed in my (very) old post, it is not difficult to write such a function yourself. I don't expect RunRev to provide such a function, because it would bring forth an unlimited number of variations on such a request.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

June
Posts: 2
Joined: Tue May 01, 2012 1:33 am

Re: array functions (length, sort, push, pop, shuffle)

Post by June » Tue May 01, 2012 5:36 am

Hi mark -

Actually your very old post does not offer a solution to shuffle because, as you pointed out, using random does not guarantee that all keys would be used and that no keys would be duplicated.

I did however come up with the following code, which does, indeed, function as I wish. In this particular array, I know I have 64 keys/items.

put 64 into z
put "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64" into test
set the itemdelimiter to ","
repeat with i = 1 to 64
put random(z) into y
put item y of test into b
delete item y of test
-- manipulate key of array
put z-1 into z
end repeat

Maybe this will help someone else.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5145
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by Mark » Tue May 01, 2012 9:43 am

Hi June,

Actually, I think that my solution should work in your specific case. If you really don't like it, you might want to do this:

Code: Select all

put the keys of myArray into myList
repeat number of lines of myList
   put random(number of lines of myList) into myKeyNr
   put line myKeyNr of myList & cr after myNewList
   delete line myKeyNr of myList
end repeat
This script puts a randomly sorted list of keys into variable myNewList. It works independently of the number of keys in the array and avoids using an additional z variable.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3130
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by mwieder » Tue May 01, 2012 5:37 pm

June- Mark's original implementation does indeed do the same job, and has the advantages of being much faster (the "repeat for each" construct), and of working with any size array, not just a hard-coded value of 64 elements.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5600
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by jacque » Tue May 01, 2012 5:42 pm

Or the ever-popular:

Code: Select all

put the keys of myArray into myList
sort myList by any line of myList
This does a random shuffle.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

fgaudreau
Posts: 2
Joined: Wed Nov 26, 2014 8:35 am

Re: array functions (length, sort, push, pop, shuffle)

Post by fgaudreau » Sat May 09, 2020 7:42 am

Fastest possible random shuffle

sort lines of myKeys by random(9007199254740991)

Actually it works almost as well with any other number above 1000, but there is no gain in speed, so why not go for the best possible random distribution with the highest number that happens to work. Unlike the delete line method, it doesn't take longer as the array gets larger.

And yes, sorting keys by random does guarantee that there will be no duplicates, I have tested this. The only drawback is that it may occasionally produce similar shuffles. Using a big number makes similar shuffles impossibly rare, like winning the lottery twice in a row.

Cheers everyone,
Francois

Klaus
Posts: 12007
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by Klaus » Sat May 09, 2020 12:17 pm

A BOT?
Or just a BIT (eight years!) late for the party?
8)

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5145
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by Mark » Sat May 09, 2020 1:08 pm

I fear this isn't a bot. :?
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Klaus
Posts: 12007
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by Klaus » Sat May 09, 2020 1:13 pm

Yes, the content of the posting seems to proof that.
However after 8 (EIGHT) years... 8)

bogs
Posts: 4790
Joined: Sat Feb 25, 2017 10:45 pm

Re: array functions (length, sort, push, pop, shuffle)

Post by bogs » Sat May 09, 2020 1:19 pm

Well, I myself don't see anything wrong with that, but I know not everyone else feels that way. I actually often enjoy these blasts from the past myself hah.

Nice to see you here Mark, it has been a long time :D
Image

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3130
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: array functions (length, sort, push, pop, shuffle)

Post by mwieder » Sat May 09, 2020 6:55 pm

...but since we're all ducks here...

some years ago a friend wanted a construct where he could pop values off the top of the stack or pull them off the bottom, a mixture of a first-in-last-out and first-in-first-out stack. Since this ended up having the features of both a queue and a stack...

he named it a 'quack'.

bogs
Posts: 4790
Joined: Sat Feb 25, 2017 10:45 pm

Re: array functions (length, sort, push, pop, shuffle)

Post by bogs » Sat May 09, 2020 7:04 pm

You obviously need a time out, that joke was almost on my level of humor :P
Image

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”