Script Dictionary, Switch, and If
HegemonKhan
01 Jan 2014, 15:48another quick question
(I'm mostly just checking ~ making sure ~ verifying my, hopefully correct, understanding of the Script Dictionary function)
is there any significant difference between these (as I presume to understand that these all have basically the same functionality) ???
or, are they not related at all to each other in what they do in terms of functionality ???
(I'm mostly just checking ~ making sure ~ verifying my, hopefully correct, understanding of the Script Dictionary function)
is there any significant difference between these (as I presume to understand that these all have basically the same functionality) ???
or, are they not related at all to each other in what they do in terms of functionality ???

jaynabonne
02 Jan 2014, 10:12They all can, in certain circumstances, be used for the same sort of thing, but they are only similar where they happen to overlap. Outside of a switch statement (which is just a more concise form of a *specific case* of an if/else), they all have their own unique applications that don't overlap. As an analogy, you can use subtraction to perform integer division, but that doesn't mean subtraction and division are the same functionality.
An "if" statement allows conditional code to be executed. It takes a Boolean expression which is evaluated and if the value is true, then the statements within the block are executed. This can be used for any Boolean gating of program flow and is a fundamental construct.
A "switch" statement is an alternate form of a *specific case* of a multiple if/else statement, where you have a single value that you are comparing to multiple values, with statement blocks executed for the various cases. You can always rewrite a switch with if/else's.
A script dictionary is a data construct that maps keys to scripts. It can be used for whatever you wish. There is not necessarily even the notion of code being executed with a script dictionary (just as there is no implicit requirement about what you do with the value you get from any other dictionary). You could look up a script value and assign it to a variable for later use, for example, where such use possibly never even becomes necessary.
One thing to be careful about is not to confuse the "statement blocks" of the if/else/switch statements with a script in its own right. A script is a data entity that consists of a statement block as well, but what you see in an if statement is not "a script" as such. The important distinction here is that of context. Statement blocks for if/else/switch statements are executed within the context of the containing block. So all defined variables are visible. When you invoke a script, the statements within that script are executed in an entirely new context. That means that you could not, for example, *generally* replace a switch statement with scripts executed from a script dictionary, as the overall execution context would be lost if scripts were used. To that extent, especially, they are very much not equivalent.
An "if" statement allows conditional code to be executed. It takes a Boolean expression which is evaluated and if the value is true, then the statements within the block are executed. This can be used for any Boolean gating of program flow and is a fundamental construct.
A "switch" statement is an alternate form of a *specific case* of a multiple if/else statement, where you have a single value that you are comparing to multiple values, with statement blocks executed for the various cases. You can always rewrite a switch with if/else's.
A script dictionary is a data construct that maps keys to scripts. It can be used for whatever you wish. There is not necessarily even the notion of code being executed with a script dictionary (just as there is no implicit requirement about what you do with the value you get from any other dictionary). You could look up a script value and assign it to a variable for later use, for example, where such use possibly never even becomes necessary.
One thing to be careful about is not to confuse the "statement blocks" of the if/else/switch statements with a script in its own right. A script is a data entity that consists of a statement block as well, but what you see in an if statement is not "a script" as such. The important distinction here is that of context. Statement blocks for if/else/switch statements are executed within the context of the containing block. So all defined variables are visible. When you invoke a script, the statements within that script are executed in an entirely new context. That means that you could not, for example, *generally* replace a switch statement with scripts executed from a script dictionary, as the overall execution context would be lost if scripts were used. To that extent, especially, they are very much not equivalent.
HegemonKhan
02 Jan 2014, 16:37This is a bit above my understanding of code, as it feels like, if I may call it, "code logic theory". I'm reluctant to say that I got the meaning of the context you were trying to explain to me, laughs, as I think that went right over my head. I'm not ready to understand coding at this depth yet, lol.
I didn't explain what I was trying to ask about at all with this thread, so my apologies for not trying to specify what answer I was seeking at.
...how to try to describe this question of mine...
Can the script dictionary be used to do the same thing as a "switch" (or multiple "ifs"), and thus lessening the amount of lines used for coding it, as both "switches" and layers of "ifs" accumulates usage of a lot of lines, whereas if I understand the "script dictionary", I could do the same thing, with using just a single line of code ???
I didn't explain what I was trying to ask about at all with this thread, so my apologies for not trying to specify what answer I was seeking at.
...how to try to describe this question of mine...
Can the script dictionary be used to do the same thing as a "switch" (or multiple "ifs"), and thus lessening the amount of lines used for coding it, as both "switches" and layers of "ifs" accumulates usage of a lot of lines, whereas if I understand the "script dictionary", I could do the same thing, with using just a single line of code ???

jaynabonne
02 Jan 2014, 18:46The answer is: if certain conditions exist. If you have a switch statement where none of the cases set any local variables (e.g. they all just go off and do something to global state somewhere) so that they are not dependent on the local function's context, then they could all be put a script dictionary indexed by the key values you would use in your cases. It would be two lines - one to look up the script, and the other to invoke.
Basically, the fact that you're asking the question means to me that you think it's possible or have a hint it will. I'd say, if so, try it out! You'll find your answer, and it will make more sense in the long run.
Basically, the fact that you're asking the question means to me that you think it's possible or have a hint it will. I'd say, if so, try it out! You'll find your answer, and it will make more sense in the long run.

HegemonKhan
02 Jan 2014, 19:31I'm getting to testing and playing around with script dictionaries, just wanted to ask on here about it first ~ being lazy ~ get an answer from you guys (or just you jayna, lol), well the post~thread has some value, as someone else may be interested in it as well. I'm still busy first with working on date, time, seasons, and other calender stuff, but next is to practice using the script dictionaries.