Page 1 of 2
array functions (length, sort, push, pop, shuffle)
Posted: Mon Feb 26, 2007 7:17 pm
by merrillp
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
Re: array functions (length, sort, push, pop, shuffle)
Posted: Mon Feb 26, 2007 11:56 pm
by Mark
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
Re: array functions (length, sort, push, pop, shuffle)
Posted: Tue May 01, 2012 1:48 am
by June
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?
Re: array functions (length, sort, push, pop, shuffle)
Posted: Tue May 01, 2012 2:01 am
by Mark
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
Re: array functions (length, sort, push, pop, shuffle)
Posted: Tue May 01, 2012 5:36 am
by June
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.
Re: array functions (length, sort, push, pop, shuffle)
Posted: Tue May 01, 2012 9:43 am
by Mark
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
Re: array functions (length, sort, push, pop, shuffle)
Posted: Tue May 01, 2012 5:37 pm
by mwieder
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.
Re: array functions (length, sort, push, pop, shuffle)
Posted: Tue May 01, 2012 5:42 pm
by jacque
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.
Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 7:42 am
by fgaudreau
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
Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 12:17 pm
by Klaus
A BOT?
Or just a BIT (eight years!) late for the party?

Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 1:08 pm
by Mark
I fear this isn't a bot.

Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 1:13 pm
by Klaus
Yes, the content of the posting seems to proof that.
However after 8 (EIGHT) years...

Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 1:19 pm
by bogs
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

Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 6:55 pm
by mwieder
...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'.
Re: array functions (length, sort, push, pop, shuffle)
Posted: Sat May 09, 2020 7:04 pm
by bogs
You obviously need a time out, that joke was almost on my level of humor
