seems to meny If

Alex Tweedly alex at tweedly.net
Fri Nov 25 04:39:01 EST 2005


Ken Ray wrote:

>On 11/24/05 8:52 PM, "liamlambert" <liamlambert at mac.com> wrote:
>
>  
>
>>how do I write this in a switch statement or is there a better way
>>to write it
>>    
>>
>Well, first of all, I wouldn't name any buttons with numbers - it can
>confuse Rev - use something like "B1" or whatever you like. And secondly, I
>would put whatever is in "it" into some other variable so you can make sure
>it doesn't get messed with.
>
>  
>
I agree with everything Ken said - but wanted to show a couple more 
versions (just to prove Rev always offers many choices of how to solve 
something :-)

>My solution below uses B1-B12 instead of 1-12, and has put "it" into tData:
>
>put it into tData
>put "JK,AB,JL,JM,GK,HK,IK,GL,HL,IL,GM,HM,IM,HN,IN,GN" into \
>  tPairs
>put "NextTrack,NextTrack,playpause,back,B1,B2,B3,B4,B5," & \
>  "B6,B7,B8,B9,B10,B11,B12" into tBtns
>repeat with x = 1 to the number of items of tPairs
>  if tData contains (item x of tPairs) then
>    do "send mouseUp to btn " & quote & (item x of tBtns) & quote
>end repeat
>  
>
For me, it's fatal to use two "parallel" lists like that - sooner or 
later, I'll want to add, delete or move one of the entries, and 
mis-count in the other list. Also, I find it easier to read and 
comprehend if I keep the string I'm matching directly adjacent to the 
name of the button, so I'd do something like

put it into tData
put "JK NextTrack,AB NextTrack,JL playpause,JM back" into tPairs
put comma & "GK B1,HK B2,IK B3,GL B4,HL B5,IL B6,GM B7,HM B8" after tPairs
put comma & "IM B9,HN B10,IN B11,GN B12" after tPairs
repeat for each item X in tPairs
  if tData contains (word 1 of X) then
    do "send mouseUp to btn " & quote & (word 2 to -1 of X) & quote
end repeat

I might even do ....
put "JK  NextTrack," into tPairs
put "AB  NextTrack," after tPairs
put "JL  playpause," after tPairs
....
put "GN  B12" after tPairs

which is kind of long winded, but has the advantage of being very easy to scan visually later.  For similar reasons, I'd consider putting them in alphabetical order by the "index" string, rather than grouping by the "effect" button name - more likely to be helpful when I come back to look at this bit of code in a year's time (depends on where the "JK", "AB", etc are coming from).



Or, I might put lCommands into a script-local variable and do
(in initialization code)

put "JK NextTrack,AB NextTrack,JL playpause,JM back" into lCommands
put comma & "GK B1,HK B2,IK B3,GL B4,HL B5,IL B6,GM B7,HM B8" after lCommands
put comma & "IM B9,HN B10,IN B11,GN B12" after lCommands
split lCommands with comma and space

(in running code)

put it into tData
if lCommands[tData] is not empty then
   do "send mouseUp to btn " & quote & lCommands[tData] & quote
end if



Which of these I'd choose probably depends, honestly, mostly on the mood 
I'm in at the time :-)

I could however probably put together a half-way convincing claim that 
the choice would be made according to the likelihood of subsequent 
maintenance efforts, and finding the optimum balance between ease of 
reading (by programmer) vs conciseness vs speed vs ...

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.362 / Virus Database: 267.13.4/176 - Release Date: 20/11/2005




More information about the use-livecode mailing list