Page 1 of 1

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

Posted: Thu May 26, 2011 3:42 am
by awesomeandrew
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?

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

Posted: Thu May 26, 2011 4:44 am
by dglass

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

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

Posted: Thu May 26, 2011 2:37 pm
by dunbarx
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

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

Posted: Sat May 28, 2011 6:36 pm
by awesomeandrew
Awesome--your replies are both extremely helpful. Thanks! :P