array functions (length, sort, push, pop, shuffle)
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
array functions (length, sort, push, pop, shuffle)
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
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
Re: array functions (length, sort, push, pop, shuffle)
Dear Merrillp,
1) a function to find the number of elements in an array (length)
2) a function to push a value onto the end of an array (push)
(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:
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:
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
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)
Code: Select all
put myArray[last line of the keys of myArray]
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]
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: array functions (length, sort, push, pop, shuffle)
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?
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?
Re: array functions (length, sort, push, pop, shuffle)
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: array functions (length, sort, push, pop, shuffle)
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.
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.
Re: array functions (length, sort, push, pop, shuffle)
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:
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
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: array functions (length, sort, push, pop, shuffle)
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.
-
- VIP Livecode Opensource Backer
- Posts: 7257
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: array functions (length, sort, push, pop, shuffle)
Or the ever-popular:
This does a random shuffle.
Code: Select all
put the keys of myArray into myList
sort myList by any line of myList
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: array functions (length, sort, push, pop, shuffle)
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
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
Re: array functions (length, sort, push, pop, shuffle)
A BOT?
Or just a BIT (eight years!) late for the party?
Or just a BIT (eight years!) late for the party?
Re: array functions (length, sort, push, pop, shuffle)
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: array functions (length, sort, push, pop, shuffle)
Yes, the content of the posting seems to proof that.
However after 8 (EIGHT) years...
However after 8 (EIGHT) years...
Re: array functions (length, sort, push, pop, shuffle)
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
Nice to see you here Mark, it has been a long time
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: array functions (length, sort, push, pop, shuffle)
...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'.
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'.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: array functions (length, sort, push, pop, shuffle)
You obviously need a time out, that joke was almost on my level of humor