de en es fr
Let the machine help
Light teasing, exhibition, BDSM, sissyfication, watersports... with sounds and pictures


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 How do you shuffle an array in Groovy?
PostPosted: Mon Sep 12, 2016 9:44 am 
Offline
Veteran
User avatar
I've spent hours today trying to find a way to do this "right". All I want to do is "shuffle" a list of strings within an array without affecting other arrays.

For example:
Code:
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
def arrayExample = arrayOriginal


First, I read about "Collections.shuffle" online but that seems to behave unexpectedly for me:
Code:
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
def arrayExample = arrayOriginal
Collections.shuffle(arrayExample)

... seems to shuffle both arrayExample *AND* arrayOriginal, though I have no idea why!

So I tried (based on a Farkel script snippet that seemed to do what I wanted)...
Code:
def shuffleArray = {
   array ->
   for(int i=0; i<array.size(); i++){
      int s = i+(int)(getRandom(array.size()-i))
      def temp = array[s]
      array[s] = array[i]
      array[i] = temp
   }
}
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
def arrayExample = arrayOriginal
shuffleArray(arrayExample)

... but that ALSO still seems to shuffle arrayOriginal too! :(

What am I missing/screwing up here?

I've "fixed" my script by simply moving the shuffle function *after* the array is show to the player (rather than before) and this works, but I would still like to know how to do this without having to work around things if possible, to learn better coding.


Top
 Profile Send private message 
 
 Re: How do you shuffle an array in Groovy?
PostPosted: Mon Sep 12, 2016 10:11 am 
Offline
Veteran
User avatar
I think the problem is your variables are "pointers" so in

Code:
def arrayOriginal = ["blue","red","green","yellow","orange,"pink"]
arrayExample = arrayOriginal


This will make arrayExample just point to the same address in memory as arrayOriginal so changing the values will change both. I think the trick is to use a function to duplicate the array instead of "=" so would be something like:

Code:
arrayExample = arrayOriginal.collect()


Not at desktop just now so may not be right... but hope it points in the right direction ;-)

_________________
Liz

You can't take something off the Internet - it's like taking pee out of a pool.
https://play-clan.site profile: Liz


Top
 Profile Send private message 
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: Google [Bot], Yandex [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010 - 2020
This website uses session cookies only.