[ANN] Property List Table

Ken Ray kray at sonsothunder.com
Sat May 28 00:45:28 EDT 2005


On 5/27/05 10:01 PM, "Dennis Brown" <see3d at writeme.com> wrote:

> I doubt my scripts would be a model of superb Transcript.  Perhaps
> someone can take a look at it and suggest better ways of doing this
> simple table task.  It seemed very clunky to me for the way I figured
> out how to click on the header rows for a sort --but it was quick and
> dirty.

If you don't mind, I'd like to take a stab at it... here's your original
script:

on mouseup
  get me
  if the clickChunk is not empty then
    do "put "&the clickChunk&" into x"
    put "wd,cd,grp,btn,fld,sb,grc,img,player,EPS,vc,ac" into objList --wd
    repeat with i=number of items of objList+1 down to 0
      if x contains item i of objList then exit repeat
    end repeat
    set itemdel to tab
    sort it
    if i>0 then sort lines of it descending by item i+1 of each
    put it into me
  end if
  set the textcolor of line one of me to blue
  repeat with i=2 to the number of lines of me
    if i mod 2 = 0 then get darkred else get black
    set the textcolor of line i of me to it
  end repeat
end mouseup

Here's what I suggest as a more optimal way (watch the line wraps):

on mouseUp
  put itemOffset(the
clickText,"wd,cd,grp,btn,fld,sb,grc,img,player,EPS,vc,ac")+1 into tItemNum
  set itemDel to tab
  put me into tData
  sort tData
  if tItemNum > 1 then sort lines of tData descending by item tItemNum of
each
  put tData into me
  set the textColor of line 1 of me to blue
  repeat with i = 2 to (the number of lines of me) step 2
    set the textColor of line i of me to darkred
  end repeat
end mouseUp

You can use is the clickText instead of the clickChunk so you can get the
text that was clicked directly. By using 'itemOffset', you can directly get
the comma-delimited item that matches the clickText, and add 1 to it. If the
text isn't found, it will return 0; 0+1 = 1.

Since you already have the location of the matching column in tItemNum, you
can use it to do the sort.

Finally, the 'repeat' control structure allows you to skip every x number of
lines in the data by using the 'step' option; here, I'm using 'step 2' to
make sure that it only handles the even numbered lines. Since by default the
text is black when it is put into the field, you only need to set the blue
and red lines.

I also noticed that you use 'it' a lot (as a result of a 'get'). Since 'it'
is a temporary variable, it's lifespan before it gets changed is usually
very small. So I would recommend using 'put' instead of 'get', depositing
the result into a variable of your own naming (I used 'tData' and 'tItem'
here).

Your sorting routines work because you have an inserted space before
"Property Name", and all the other columns are either X's or blanks other
than the header. This is quite clever! However what might have been better
would be to separate the header into another field that was placed atop the
field with the data in the group. Clicking on the header would cause the
*other* field to sort; the header would be left alone.

Regardless of my attempt at optimizing your scripts, I must say that I am
very encouraged at your rate of progress. Keep up the good work!

HTH,
 
Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com



More information about the use-livecode mailing list