SelectedChunk

Peter Brigham MD pmbrig at gmail.com
Mon Sep 27 15:55:34 EDT 2010


You can work out exactly what you want if you realize that the result  
of "the selectedChunk" is always char <chartPosition> to <endPosition>  
of field <n>, and this will be as you would expect if the selection is  
not empty:

ie, in "this is a sample string of text" in field 1, selecting the  
characters "is a sample" would result in a selectedchunk of "char 6 to  
16 of field 1"
in which case the number of words of char 1 to (word 2 of the  
selectedchunk) of field 1 would be:

the number of words of char 1 to 6 of field 1,
ie, the number of words of "this i"
which is 2

and if the selection is empty then indeed the selectedchunk is always  
of the form "char <n> to <n-1> of field 1":

So putting the insertion point before "is" in the above example would  
result in a selectedchunk of
"char 6 to 5 of field 1"
and therefore the number of words of char 1 to (word 2 of the  
selectedchunk) of field 1 would be

the number of words of char 1 to 5 of field 1,
ie, the number of words of "this "
which is 1

On Sep 27, 2010, at 1:53 PM, Mike Bonner wrote:

> Nevermind. *sigh* Number of words prior to selection or insertion  
> point is
>      put the number of words of (char 1 to (word 2 of the  
> selectedchunk) of
> field 1)
>
> Rev is far more amazing than I can make it be.  So is Livecode!

Yes indeed. Once you get the hang of chunk expressions you can do  
*lots* of things -- auto-format phone numbers, insert boilerplate at  
particular points in a field, extract specific information from a  
field or other container, etc. I have a little function to get the zip  
code for an address by sending the address to google maps, fetching  
the htmltext of the resulting URL, parsing it to get the zip code from  
the middle of the htmltext, and returning the zip -- all behind the  
scenes without displaying the webpage. Takes less than a second with a  
fast internet connection, the zip code is inserted after the address,  
and the user never needs to know what goes on behind the scenes.

> On Mon, Sep 27, 2010 at 11:50 AM, Mike Bonner <bonnmike at gmail.com>  
> wrote:
>
>> Ok thats cool.
>> So if its NOT just an insertion point then
>> the number of words of char 1 to (word 2 of the selectedchunk) of  
>> field
>> "yourfield"
>> will give the number of words preceeding the selection?

Yes, but see above -- it would include the word that starts the  
selection.

>> So this whole thing could boil down to
>>
>> on mouseUp
>>   if word 4 of the selectedchunk < word 2 of the selectedchunk then
>>      put the number of words of (char 1 to (word 4 of the  
>> selectedchunk)
>> of field 1)
>>   else
>>      put the number of words of (char 1 to (word 2 of the  
>> selectedchunk)
>> of field 1)
>>   end if
>> end mouseUp

Pretty much.

>> I knew I was making my solution WAY too difficult. Thanks for this.

No problem.

Check out the dictionary entries for not only selectedchunk but also  
offset, wordoffset, itemoffset, and related terms, then do some  
experimentation. Text parsing in Rev, er, LiveCode, is very powerful.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list