how do you randomize words in a field without re-using?

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
awesomeandrew
Posts: 5
Joined: Wed May 04, 2011 12:19 am

how do you randomize words in a field without re-using?

Post by awesomeandrew » Thu May 26, 2011 3:42 am

Say you have a field with a few paragraphs of text and you want to randomize the text and put it in another field. This is just for fun. Here is what I have:

on mouseUp
repeat with x = 1 to the number of words in field "originalText"
put any word in field "originalText" & space after field "newText"
end repeat
end mouseUp

I want to use each word once and not re-use any words. So where do I go from here?

dglass
Posts: 519
Joined: Thu Sep 24, 2009 9:10 pm
Contact:

Re: how do you randomize words in a field without re-using?

Post by dglass » Thu May 26, 2011 4:44 am

Code: Select all

on mouseUp
   put field "Field1" into myVar
   
   set itemDelimiter to space
   
   sort items of myVar by random(number of items of myVar)
   
   put myVar into field "Field2"
   
end mouseUp

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

Re: how do you randomize words in a field without re-using?

Post by dunbarx » Thu May 26, 2011 2:37 pm

The solution from dGlass is likely the most elegant.

But just to elaborate on your own effort, when you selected a word from your text at random, you provided no means to delete that word from the whole. One slightly more clunky method, but one that will show you the idea, is

Code: Select all

on mouseUp
      put field "yourField" into myVar
   repeat the number of words of myVar
      get random(the number of words of myVar)
      put word it of myVar & space after temp
      delete word it of myVar
   end repeat
      answer temp
end mouseUp

awesomeandrew
Posts: 5
Joined: Wed May 04, 2011 12:19 am

Re: how do you randomize words in a field without re-using?

Post by awesomeandrew » Sat May 28, 2011 6:36 pm

Awesome--your replies are both extremely helpful. Thanks! :P

Post Reply