switch vs .. not sure what to call it.

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

switch vs .. not sure what to call it.

Post by sturgis » Wed Mar 25, 2009 3:58 am

I've been working on an app lately that runs a handler after clicking in a list, based on the text in the list.

Its setup using switch and case.

Would it make sense/be more efficent to drop the text in question into a variable sans spaces, and have all my handlers/functions named that way?

What I mean is, say the clicked text has "Find Payment" remove the space, shove FindPayment into a temp spot and then

do varname

thus bypassing the need for switch and case entirely?

Are there situations where one method is superior to another and vice versa?

Any input is welcome. The doohickey i'm workin on is becoming a bit complicated (for me) and i'm hoping to figure out simplification methods, early and often.

Thanks in advance for any responses.

edit: How do I check to see if a handler exists?
Is it possible to create new handlers on the fly from script? Even in standalone?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Mar 25, 2009 9:24 am

Dear Sturgis,

It looks to me that you are making things waaaay too complex. Why do you think that you have a need to put handler names into variables before running those handlers? Why would you have a need to change handlers on the fly?

If you really want it, you can change handlers on the fly in the IDE without limits, but such handlers are limited to 10 lines in standalones.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Wed Mar 25, 2009 3:31 pm

Currently I have this to determine which handler to run.

Code: Select all

on mouseUp
   put the value of the selectedChunk into funcName -- loads selected text for later use
   --execute a function based on selected list box line text
   switch the value of the selectedChunk
      case "Find Payment"
         do fPaymentScreen
         exit switch
      case "Find Future Value"
         do fValueScreen
         exit switch
   end switch
end mouseUp
While I only have 2 handlers for 2 different formulas to eval (and screen setup and what not for output) the switch block is simple and easy.

Was thinking that once I fill things out a bit, the switch block will become unwieldy.

I think I see that I wouldn't need a variable to do the alternate method just something like

Code: Select all

 
do -- code to strip spaces to make the selected chunk match the handler name here.  Without the variable step.  
i'd rename the 2 handlers I already have to match my static text chunks in the list field. At which point any time I add new functionality, just add the static text, and make sure I name the handler right.

Am I on the right track here?


As for creating handlers on the fly, I was thinking of making it so that more capabilities could be added from within the app itself.

Ask for the name of whats to be added, the required inputs, the formula with placeholders for the inputs.
Add the name to the listfield, create a handler on the fly to handle everything.

While typing this it has occured to me that there might be an answer thats easier.

Could I store all the pertinent information for each formula including number and description of all required data, and the formula itself and use a single handler to do all of the formula evaluations?

If I had something like (simple example) 3 + field "field1" * (field "field2" / field "field3") in the database (doing all the gymnastics to handle proper quoting) would everything expand properly so that I could

Code: Select all

put databaseStuff into field "outField"
[code]
and get the correct result?   Assuming the formula syntax  and inputs are valid of course.  

Thanks for all the help.  I'm probably trying to get way too deep for being so new, but the more info I can pound into my brain the sooner things will start to gel. 

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Mar 25, 2009 3:46 pm

Dear sturgis,

The second part of your message is indeed a good description of a correct strategy. You can change the behaviour of a handler by means of parameters, field data, custom properties, button hilites et cetera. This allows you to use the same handler, without the need to create a new handler.

As the first part, I think that although a switch controls structure is more verbose, it will be easier to maintain. You will probably encounter problems eventually, that make it less viable to call handlers with the do command. I use the do command only by exception, as a means of last resort.

Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Wed Mar 25, 2009 5:59 pm

Thanks so much for all your help. I have 1 or 2 more things to clear up, but have a test stack working and much simpler than before.

I have 2 more questions if you don't mind.

If I set things up so I can build a formula up using placeholders for where user supplied data will be placed, is there a way to replace multiple different strings with their replacements without 5 seperate (or nested replaceText) replacements?

My second question deals with field names themselves. As long as the field name doesn't contain spaces or strange charaters is it safe to refer to them without quotes?

field fldname works, I just don't want to run into some hidden gotcha down the road.

Thank you very very much for letting me pick your brain.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Mar 25, 2009 6:18 pm

Hi Sturgis,

First and most of all, you should always, without exception, put quotes around object names. Strings without surrounding quotes may be interpreted as variables and may cause errors.

I can't answer your question about replacements. Perhaps you should post your script here and let me see if there is anything that can be improved.

Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Wed Mar 25, 2009 6:52 pm

I've managed to shrink my replace down to 1 line and still do what I want with one exception. Follows is the code i'm using on a test stack.

Code: Select all

on mouseUp
   put "field1 + field3 + field3 + field3" into formula
   replace "field" with "field" && "fld" in formula
   put the value of formula into field "outFld"
end mouseUp
This works fine, and I assume I could just have my formula place keepers be the name of the field itself now that I think on it.

Actually think I just answered my own questions.

I think I read somewhere else in the forum that putting _quote_ in as a placeholder and replacing it with quote is a way to simplify some things.

So if I had a formula string as below, and replaced _quote_ with quote, i'd end up with a) something thats easy to store and retrieve from an sqlite db, and b) easy conversion back to a functioning formula including object quotes.

Code: Select all

put "field _quote_fld1_quote + field _quote_fld3_quote + field _quote_fld2_quote_" into formula 
-- this would be a database query, but one thing at a time.
replace _quote_ with quote in formula
answer the value of formula
Am I getting closer to the right track?

edit: If I have this code..

Code: Select all

on mouseUp
   put "field _quo_fld1_quo_ + field _quo_fld2_quo_ + field _quo_fld3_quo_ + field _quo_fld3_quo_" into formula
   replace "_quo_" with quote in formula
   put formula && "=" && the value of formula
end mouseUp

It outputs the formula including field names, and the result just fine.
Is there a way to output the formula with the fields replaced by their values?

I want an output like
100 + 200 + 300 = 600
rather than field "fld1" + field "fld2" + field "fld3" = 600

It looks like merge should do what I want if I can figure out how to arrange things but no luck so far.

Again, thx for all your help Mark, and everyone.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Mar 26, 2009 3:06 am

Dear Sturgis,

I think that

Code: Select all

the value of the value of formula
will give you the desired result. I think you can do this simpler, though:

Code: Select all

repeat with x = 1 to 5
  put fld ("fld" & x) & space & "+" & space after formula
end repeat
delete char -3 to -1 of formula
put formula && value(formula)
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Thu Mar 26, 2009 3:53 am

The repeat loop would duplicate my example very easily I think, but isn't quite what i'm shooting for. Mind if I pester you with questions still more?

Let me describe my app, and where my current holdups are.

Basically i'm building a financial cookbook. Can't use that name because there was an app of the same name on Atari ST years ago.

Main stack is just a list of what can be done (only 2 things there so far, and i've ripped out most of the scripts to rework things) Click a line in the list field, it opens a drawer with the apropos number of fields and labels for input.

The informatio for the drawer table would be kept in a database or file, number of required fields for that particular formula, the labels, the name of the formula itself, and the formula string. The lines of the choice listbox would be populated from the database.

The drawer screen would take all the info from the selected record, set itself up, and take the pre-built formula string and do its gymnastics.

Currently i'm populating things by hand, no database yet.

So I think the best question is, what should be in the database, and how much trouble am I going to discover when saving and retrieving data that has quotes, parens, brackets etc. quotes for field names, parens of course will be used in many of the formulae to force the right order of operations.

If I can get a handle on everything, then I can add new items to the list of financial functions just by popping a new entry into the database.

I think I may have bit off more than I can chew for a first real project!
Maybe its time to take a day or two and step back to let everything settle in my overloaded and smoking brain.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Mar 26, 2009 10:15 am

Dear Sturgis,

I can't tell you whether you should use a database engine or just a file. This depends on the kind of data you want to store, how you want to use these data, and from how many computers you want to access the data.

Quotes and special characters don't really have to be a problem. You'd probably better just ask questions here, when you encounter a problem.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Post by sturgis » Thu Mar 26, 2009 10:41 pm

Thanks much. Going to take a step back and let things sink in, then take a new look in a couple days.

I've already managed to do more useful things with rev and all the helpful info in the forums here than I would have thought possible.


Thanks Mark, and everyone.

If I can my project to the point where its working as I would like, any chance it could be reviewed for comments and suggestions? Mainly how to improve the background code, but also the look and feel as well. This assumes I actually ever reach that point of course.

Thanks again all!

Sturgis

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Mar 26, 2009 11:19 pm

Dear Sturgis,

I dont mind having a quick look at a stack, later on, to get you going. If you want some serious tutoring, I believe we should discuss that off-list. Just send me an e-mail.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”