Macro Language Reference > Macros > Script - Tests > ?
  
?
Description
PTC ALD script extension has a condition tester that allows macros to be executed only when the condition is true. As with labels and jumps the condition tester, in this form, is only available within scripts executed with trun. Examples of all conditions and their operators are documented below.
 
Example 199. Test a single numeric value
Four examples of testing a single numeric value are detailed below, with possible actions that can be executed as a result of the test:
?0^count }finish
If value of ^count is zero, jump to label finish
?+^num trun --1
If value of ^num is positive, exit script
?-^say @hello
If value of ^say is negative, insert text hello
?+-^end {loop
If value of ^end is non-zero, jump to label loop
 
Example 200. Test two numeric values
Five syntax examples of testing two numeric values including absolute values, variable values and combined values are detailed below:
?=^var1,^var2 }finish
If ^var1 is equal to ^var2, jump to label.
?^var1,^(1) tpgoto 100
If ^var1 is greater than 1, go to page 100
?-^var1,^(^page+^count) {top
If ^var1 is less than total of ^page plus ^count jump to label
?=^char,'a' @Letter 'a' was pressed
If ^char is equal to the Keycode of 'a', insert text "Letter 'a' was pressed"
?= 1,^value @Value is -1
If ^value is -1, insert text "Value is -1"
 
Example 201. Compare two strings of text for equal, not equal or empty conditions
?=^string1,^string2 @Streams are equal
If ^string1 is the same as ^string2 insert text "Streams are equal"
?+-^string1,^string2 (^string1=Strings were different):
(boxit \^string1^)
If ^string1 is not the same as ^string2 change ^string1 to read "Strings were different" and display the result on-screen using boxit.
?=^string," "}empty_string
Test if ^string is empty. If it is empty, jump to label empty_string.
:empty_string
The :empty_string label.
 
Example 202. Test if last page of document has an even or odd number
Because numeric variables are always whole numbers, dividing the number you want to test by two and then multiplying it by two will produce different results, according to whether the number was odd or even. The example below finds out if the last page of a document is even or odd:
getvar 01535 "lastpage"
^tmp=^lastpage
^tmp/2
^tmp*2
^tmp-^lastpage
?=^tmp boxit \^lastpage is even^
?-^tmp boxit \^lastpage is odd^
1) Uses getvar to create a variable called ^lastpage.
2) Copies ^lastpage into a variable called ^tmp.
3) Divides ^tmp by two. The result is stored without its decimal fraction, e.g. ?=2.
4) Multiplies ^tmp by two.
5) Subtracts the original value of ^lastpage from the modified value of ^tmp.
6) If ^lastpage was even, ^tmp and ^lastpage will still have the same values. Taking one from the other will give zero. Again, we will use boxit to display the result on screen. In this case the message will actually show the number that was tested, and so we include the ^lastpage variable in the message. boxit follows the rules for special characters that are used in dialogue box definitions and we therefore have to tell it treat the first ^ as a variable using the \ (backslash) character.
7) If ^lastpage was odd, ^tmp will be one less than ^lastpage. Display the results via boxit.
 
Example 203. Test if the variable ^num contains an integer number in the range 1 to 20
Verify strings let you test a string for certain criteria, and return a result of true or false depending on whether the string matched the given criteria or not.
This example of the I option tests if the variable ^num contains an integer number in the range 1 to 20 and, if so, display to the screen.
^num=20
?=#^num,"1..20i"}show_me
?+#^num,"1..20i"}exit
:show_me
boxit ^num is valid^
:exit
trun 0
1) Set the variable ^num to the value 20
2) If ^num contains an integer in the range 0 → 20 then jump to label :show me
3) If ^num contains an integer greater than the range 0 → 20 then jump to label :exit
4) Label
5) Using boxit, display the contents of ^num and the text "^num is valid"
6) Label
7) Stop script
 
Example 204. Test using regular expressions
Regular expressions add a powerful extension to the conditional test macro. The examples below illustrate some of the many options available:
?=/^var1,"[0-5]" }loop
If ^var1 contains the numbers 1 - 5 jump to label }loop
?=/^string1,"[^a-zA-Z]" }num
If ^string1 contains anything other than letters jump to label }num
?=/^name,"a.b" @Match is good
If ^name matches the three character string a?b' output text "Match is good"
 
Example 205. Execute a macro if condition is not true
The example below tests if the ^text variable contains a tag whose name consists of characters a-z. If this condition is not true, then macro macro should be executed.
?+-#^text,"/(<[a-z]>+)"macro
 
Example 206. Test using wildcards
As shown in the example below, the wild card enables you to test for data without exact values being known:
?=*^filename,"*.3d" }end
If ^filename has the extension .3d, jump to label }end.
 
Example 207. Test using case sensitive conditional tests
In the example below, the requested macro will not be executed since a capital A will have a higher value than the lower case a:
^var1=a
^var2=A
?=@^var1,^var2 }loop
If ^var1 is equal to ^var2, jump to label }loop.
Syntax
? condition:n component1:m component2:m? macro:m
condition
0 or =
Test if element1 has a value of zero, or if element1 and element2 are equal.
+
Test if element1 has a value greater than zero, or element1 is greater than element2.
-
Test if element1 has a value less than zero, or element1 is less than element2.
#
Verify strings. For * and # the = will only be added if =, 0, + or - were not already given. This makes reverse logic tests easier.
/
Compare a regular expression. = (or 0), + and - operators will always be ignored in conjunction with this operator. The tester returns 0 (false) if the expression does not match. If there is a match, the actual match position is returned (1st char at position 1).
@
Force case sensitive comparison.
*
Use wild card on right parameter. For * and # the = will only be added if =, 0, + or - were not already given. This makes reverse logic tests easier.
' '
Allow spaces in the test : include a space after the condition.
component
^variable
Any valid variable name preceded with a ^ character.
"string"
Any valid stream name, i.e. ^tx1 refers to the variable and ^"tx1" refers to the text stream.
 
This option will only retrieve 159 characters: if the stream is longer then the surplus is ignored.
integer
Any 32 bit number, e.g. -2 147 483 or 2 147 483.
^(expres)
Any PTC ALD expression
element2
In addition to the same values as element1, element2 can also take the various test options listed below, to specify the test range and associated option:
i
Test that element is an integer in the range low...high
u
Test that element is a unit in the range low..high, where 10000 represents 1mm.
m
Test that element is a fixed point number in the range low..high, where the ranges are multiplied by 10000. i.e. 10000..20000m will check for a number in the range 1.0000 to 2.0000.
S
Test that element contains a valid tag name.
s
Test that element contains a string whose number of characters is between low...high (inclusive).
q
Test that element contains an optional unit plus a fraction of the width or height. low...high are the allowable ranges for the unit.
N
Test that element contains a valid page number. This also allows for the extended syntax of sub-page-numbers when pages have been frozen, i.e. <?show $p> instead of <?show $g>. If low.. is 1, then the separator between sub-page-numbers is '_' instead of ' . '
c
Test that the field is a valid colour description.
macro
The macro you want to run if condition is true. This can either be a normal PTC ALD macro or a { (jumpb) or } (jumpf) macro to add a jump to a certain section of your script.
If the macro contains any variables that need replacing with a value (by terminating the line with a ^) the replacement does not take place until the macro portion of the line is passed to the normal macro processor. This means that the variable(s) that make up part of the test at the beginning of the line do not get replaced.
Related Links