Page 1 of 1

shuffle list, and pop from list...

Posted: Fri May 02, 2014 9:23 pm
by tjo7777
Hi,

I'm a bit new to LiveCode, but have some experience with Perl.

Is there an easy way to create a list of items like "car, truck, bike, train, plane", then shuffle the list and pop items from the top of the list?

I'm trying to select items from the list at random without repeat selections. Is there another way to do this with LiveCode?

Thanks for any help or suggestions.

TJ.

Re: shuffle list, and pop from list...

Posted: Fri May 02, 2014 9:28 pm
by magice
There are many ways to do that here is one

Code: Select all

put "car truck bike train plane" into tList
answer word random(5) of tList
edit : oops I just saw the repeat selection requirement. Check out this thread. http://forums.runrev.com/viewtopic.php?f=7&t=20025

Re: shuffle list, and pop from list...

Posted: Fri May 02, 2014 9:40 pm
by sefrojones
this is what I came up with, there's probably a better way. Put your comma seperated list into a fld (fld 1 in my example) and put this script into a button:

Code: Select all

on mouseUp
   put the number of items in fld 1 into wordnum
   put random(wordnum) into tVar
   put item tVar of fld 1 into theWord
   answer "you picked"&& theWord
   delete item tvar of fld 1
end mouseUp
Good Luck!

--Sefro

Re: shuffle list, and pop from list...

Posted: Fri May 02, 2014 11:50 pm
by dunbarx
Hi.

I was raised from a pup by wolves and Colin Holgate. The trick is to use the fewest possible lines, for no other reason than that the English are, well, just that way, sometimes.

In a field (field 1) with several lines of text in it, and a button with this in its script:

Code: Select all

on mouseup
   delete char offset(msg,fld 1) to the length of msg  + offset(msg,fld 1)  of fld 1
end mouseup
Now all this does is put a random line of the field, which sort of addresses the need to generate an output, and then delete that line from the field, so that there are no repeats.

It may be that brevity has a cost in usability. Oh, and readability.

Craig Newman

Re: shuffle list, and pop from list...

Posted: Sat May 03, 2014 12:49 am
by magice
dunbarx wrote:Hi.

I was raised from a pup by wolves and Colin Holgate. The trick is to use the fewest possible lines, for no other reason than that the English are, well, just that way, sometimes.

In a field (field 1) with several lines of text in it, and a button with this in its script:

Code: Select all

on mouseup
   put any line of fld 1 
   delete char offset(msg,fld 1) to the length of msg  + offset(msg,fld 1)  of fld 1
end mouseup
Now all this does is put a random line of the field, which sort of addresses the need to generate an output, and then delete that line from the field, so that there are no repeats.

It may be that brevity has a cost in usability. Oh, and readability.

Craig Newman
Impressive!

Re: shuffle list, and pop from list...

Posted: Sat May 03, 2014 12:57 am
by sefrojones
Yeah, that's pretty awesome. Thanks Craig! :D

Re: shuffle list, and pop from list...

Posted: Sat May 03, 2014 8:52 am
by SparkOut
If you are going to perform repeated actions over the list, it may be even simpler to create the randomised list in advance:

Code: Select all

sort lines of field 1 by random(the number of lines of field 1)
or

Code: Select all

sort items of field 2 by random(the number of items of field 2)
Then you can just repeat for each line / item without having to delete anything.

Re: shuffle list, and pop from list...

Posted: Sat May 03, 2014 8:58 am
by tjo7777
Thank you all very much for your help! I am truly thankful for all your great ideas and advice.

TJ.