shuffle list, and pop from list...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

shuffle list, and pop from list...

Post by tjo7777 » Fri May 02, 2014 9:23 pm

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.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

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

Post by magice » Fri May 02, 2014 9:28 pm

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

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

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

Post by sefrojones » Fri May 02, 2014 9:40 pm

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
Attachments
RandomTest.zip
(619 Bytes) Downloaded 298 times

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10335
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri May 02, 2014 11:50 pm

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
Last edited by dunbarx on Tue Jun 23, 2020 7:33 pm, edited 1 time in total.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

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

Post by magice » Sat May 03, 2014 12:49 am

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!

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

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

Post by sefrojones » Sat May 03, 2014 12:57 am

Yeah, that's pretty awesome. Thanks Craig! :D

SparkOut
Posts: 2948
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Sat May 03, 2014 8:52 am

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.

tjo7777
Posts: 34
Joined: Fri May 02, 2014 9:16 pm

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

Post by tjo7777 » Sat May 03, 2014 8:58 am

Thank you all very much for your help! I am truly thankful for all your great ideas and advice.

TJ.

Post Reply