Hey Community,
I’m starting this thread as a place to document some of the “pitfalls” of the ZPL language. The ZPL is extremely powerful but since OpticStudio is trying to create a new language, the parsing is sometimes problematic.
Variable Names
You can actually assign a value to a number (the ZPL treats the number as a variable name). So while OpticStudio won’t throw an error, you should be very careful when picking variable names:
PRINT 1 + 2 # answer is 3
1 = 2
PRINT 1 + 2 # answer is 4
FOR Loops
There is always round-off when dealing with decimals and this becomes especially true when trying to use a decimal step size in a FOR loop. Rather than using decimals as either the start, end or step size, you should keep these as integers and convert to decimals inside the FOR loop:
Recommended
FOR i = 0, 10, 1
px = i / 10
NEXT
Not Recommended
FOR i = 0, 1, 0.1
px = i
NEXT
Speed Up Calculations
All calculations via the ZPL force an update to both the GUI and the core code. Pushing data to the GUI can be extremely expensive. To get around this, I always recommend using SUSPENDUPDATES and RESUMEUPDATES:
SUSPENDUPDATES
# some really long running calculations
RESUMEUPDATES
# all subsequent code will force a core & GUI update
SWITCH Statement
The ZPL doesn’t have a built in SWITCH statement but you can mimic one by using a series of GOTO and LABEL keywords:
code$ = "grape"
GOTO code$
! default calculations go here
fruit$ = "not recognized"
GOTO END_SWITCH
LABEL apple ! note: no double quotes
fruit$ = "apples are red"
GOTO END_SWITCH
LABEL banana ! note: labels are NOT case sensitive
fruit$ = "bananas are yellow"
GOTO END_SWITCH
LABEL grape
fruit$ = "grapes are purple"
GOTO END_SWITCH
LABEL END_SWITCH
PRINT fruit$
PRINT 2D Data
By default the PRINT command will always end in a new line. If you want to continue printing on the same line, simply end the PRINT command with a comma. You can print 2D data with 2 FOR loops:
FOR i = 1, 5, 1
FOR j = 1, 5, 1
PRINT $STR(i * j), $TAB(),
NEXT
PRINT
NEXT
# output
# 1 2 3 4 5
# 2 4 6 8 10
# 3 6 9 12 15
# 4 8 12 16 20
# 5 10 15 20 25
If you have any other recommendations for the ZPL, please leave them in the comments. It always nice to learn something new :D