Chopping Up A Paragraph into chunks

J. Landman Gay jacque at hyperactivesw.com
Tue Jan 25 23:49:03 EST 2011


On 1/25/11 6:20 PM, John Patten wrote:
>
> I'm trying to divide a paragraph into random chunks, but I'm not quite
> sure how to do it.
>
> Here's what I'm thinking:

With some minor revisions your script will work but it isn't as 
efficient as it might be.

> put the text of cd fld "paragraphContainer" into tParagraph
>   put the number of words in tParagraph into tWordCount
>    put random(10) into tRandomGroup --determineshow many words per chunk
>   put tWordCount/tRandomGroup into tNumberOfWordsInChunk  --I know I could have a decimal, but I'll deal with that later...
>    put 0 into x

"div" will eliminate the decimal problem, but the variable isn't used 
after this so I'd remove that line.

>    repeat until number of words in cd fld "paragraphContainer" = 0  --Not sure this is possible?

Yes, that's valid.

>       create field
>       set the loc of last fld to (x+5,150) -- stepping the location of each new field over 5 pixels
>       select first tRandomGroup words of cd fld "paragraphContainer" -- planning to cut selected words from field and just repeat until all words are cut from field

You can select "the first word of" but you can't select "the first x 
words of". So instead you'd need to change that to "select word 1 to 
tRandomGroup words of...".

>       cut selectedText
>       paste into last cd fld
>       add 5 to x
>    end repeat

Accessing fields is one of the slowest things you can do, so it's best 
to avoid that as much as possible. The more you can do in memory, the 
better. Selecting text, cutting, and pasting are all expensive operations.

One way to access fields less is to do all the chunking in memory and 
only access fields when you need to create and put text into them. I 
might do something like this:

on mouseUp
   put the text of cd fld "paragraphContainer" into tParagraph -- don't 
touch the field again after this
   put random(10) into tWordsPerChunk -- words per chunk
   put 0 into x
   put 1 into tCurWordCount -- tracks which word we're at
   repeat until tCurWordCount > the number of words in tParagraph
     create field
     set the loc of last fld to (x+5,150)
     put word tCurWordCount to tCurWordCount+tWordsPerChunk of 
tParagraph into last fld
     add tWordsPerChunk+1 to tCurWordCount
     add 5 to x
   end repeat
   put "" into fld "paragraphContainer" -- if you want
end mouseUp

This is more efficient than cutting and pasting but it isn't as fast as 
it could be either, because each time through the loop the engine has to 
count up to the tCurWordCount word before it can find the text we want. 
So it's a little faster to just lop off the text you're done with -- 
like your original idea, only do it all in RAM:

on mouseUp
   put the text of cd fld "paragraphContainer" into tParagraph
   put random(10) into tWordsPerChunk -- words per chunk
   put 0 into x
   repeat until tParagraph = ""
     create field
     set the loc of last fld to (x+5,150)
     put word 1 to tWordsPerChunk of tParagraph into last fld
     delete word 1 to tWordsPerChunk of tParagraph
     add 5 to x
   end repeat
   put "" into fld "paragraphContainer" -- if you want
end mouseUp

You were on the right track.
-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the use-livecode mailing list