[Windows Mobile Software Development] mncessna:
interesting lua "switch" statement

i want to thank meltwater for the pointer to colossus. that really put the data together for me. i got my new clock working just fine now.

i'd like to share something i learned along the way if any one is interested in lua programming. i've been a software engineer for 28 years. actually i've never quit even though i'm retired.

i noticed that when the m9editor imports lua it has its own style for if-then-else structures...especially the indenting! yuk!

so as i was searching for answers to other lua scripting questions, i came across an interesting thought about using tables to simulate a case/switch function. i used it in my new clock. for anyone interested, here it is:

Here is how m9editor imported my structure to convert the month as a numeric string into a 3-character string:

if MonDate.String == "8" then

MonDate.String = "Aug"

else

if MonDate.String == "9" then

MonDate.String = "Sep"

else

if MonDate.String == "10" then

MonDate.String = "Oct"

else

if MonDate.String == "11" then

MonDate.String = "Nov"

else

if MonDate.String == "12" then

MonDate.String = "Dec"

else

if MonDate.String == "1" then

MonDate.String = "Jan"

else

if MonDate.String == "2" then

MonDate.String = "Feb"

else

if MonDate.String == "3" then

MonDate.String = "Mar"

else

if MonDate.String == "4" then

MonDate.String = "Apr"

else

if MonDate.String == "5" then

MonDate.String = "May"

else

if MonDate.String == "6" then

MonDate.String = "Jun"

else

if MonDate.String == "7" then

MonDate.String = "Jul"

end

end

end

end

end

end

end

end

end

end

end

end

i replaced it with this:

setMonthStr = {

[ "1"] = function (x) MonDate.String = "Jan" end,

[ "2"] = function (x) MonDate.String = "Feb" end,

[ "3"] = function (x) MonDate.String = "Mar" end,

[ "4"] = function (x) MonDate.String = "Apr" end,

[ "5"] = function (x) MonDate.String = "May" end,

[ "6"] = function (x) MonDate.String = "Jun" end,

[ "7"] = function (x) MonDate.String = "Jul" end,

[ "8"] = function (x) MonDate.String = "Aug" end,

[ "9"] = function (x) MonDate.String = "Sep" end,

["10"] = function (x) MonDate.String = "Oct" end,

["11"] = function (x) MonDate.String = "Nov" end,

["12"] = function (x) MonDate.String = "Dec" end,

}

and invoke it like this:

setMonthStr[MonDate.String](MonDate.String)

how it works: by invoking the name of the table with the MonDate.String as a parameter in square brackets setMonthStr[MonDate.String], the table returns the virtual address of the function (x) for the string index used.

then, by adding the same string in parenthesis (MonDate.String), it has the effect of calling the function (x) and passing it the MonDate.String as a parameter. works like a charm, is very clean, and is rather neat too!

i also use this technique to set the degree of rotation for my weekday graphic like this:

setWeekdayRot = {

["Su"] = function(x) wdRot = -45 end,

["Mo"] = function(x) wdRot = -90 end,

["Tu"] = function(x) wdRot = -135 end,

["We"] = function(x) wdRot = -180 end,

["Th"] = function(x) wdRot = -235 end,

["Fr"] = function(x) wdRot = -270 end,

["Sa"] = function(x) wdRot = -315 end,

}

setWeekdayRot[weekday2](weekday2)

weekdayHand.Rotation.z = wdRot

multiple statements can be included in the generic functions also. here's an example:

setWeekdayRot = {

["Su"] = function(x) wdRot = -45 wdLength = 9 end,

["Mo"] = function(x) wdRot = -90 wdlength = 8 end,

["Tu"] = function(x) wdRot = -135 wdtime = 5 xterra = -1 end,

["We"] = function(x) wdRot = -180 end,

["Th"] = function(x) wdRot = -235 end,

["Fr"] = function(x) wdRot = -270 uset = wdRot / 3 end,

["Sa"] = function(x) wdRot = -315 end,

}

there appears to be no limit, but readability forces me to keep it short! :)

also notice that each function can have different statements! it very much mimics a case/switch capability rather neatly.

DON'T MISS THE CLOSING COMMA AFTER THE LAST END!!!

I define both of these tables as global tables, that is, i declare them outside of the calendardate function so they only get created once and then re-used.

really hard to tell if there's much speed difference, but like i said, i think they look nice, easy to setup and use.

marvin