3B2 Variables
ArborX

Open the Documentation Legend

Summary

This chapter details the specifics of 3B2 variables, their use, types and application.

Introduction

In Arbortext APP, variables are named areas of memory used to store a sequence of characters and during the execution of a Arbortext APP session, numbers, strings, or arrays can be stored in variables and used in future computations.   A variable is recognised by the ^ character that must prefix the variable name, for example:

 ^varname

The names given to variables are case sensitive.  ^FRED is not the same as ^fred, likewise ^Fred is not the same as ^freD.  In order to use the variable again the name has to be exact.

Variables can be used across documents as they are retained by Arbortext APP and are only reset when the application is closed down.  All open documents in one Arbortext APP session can effectively share the same variables.

There are numerous kinds of variables in Arbortext APP, with varying syntax, which can be manipulated and used throughout the application. This chapter will describe the different aspects of working with variables, the various kinds of variables and their uses.  Below is a list of the different kinds of variable syntax that can be used in Arbortext APP.

Variable Syntax

Description

^name

Regular variable.

^name(n)

Indexed variable (array).

^attr#

SGML/XML attribute.

^attr#(n)

Indexed attribute.

^'name.ex'

Any of the above with unusual name.

^"stream"

Stream variables.

^"*script"

Mini-script.

^12345

System variables.

^12345(n)

Indexed system variables.

^(expr)

Expression.

^{string}

Indirect variable.

^[funcname parameter(s)]

Function variable.

Manipulation of variables

Variables have to be assigned a value in order for it to have any use and there are numerous ways in which this can be carried out.  Below is a list of the different types of syntax that can be used to populate a variable:

Assignment Syntax

Meaning

^var=value

Assign value to variable.

^var1=^var

Assign the value of ^var to ^var1.

^var+num

Add numeric value to variable.

^var-num

Subtract numeric value from variable.

^var*num

Multiply variable by numeric value.

^var/value

Divide variable by numeric value.

^var++

Increment variable by 1.

^var--

Decrement variable by 1.

^var!value for example: ^var!abcd

Find character length of value, for example 4.

^var!^var1

Find character length of contents of var1.

^var.var1

Concatenate variables.  (Only 2 variables at a time).

^var.food

Concatenate word 'food' to the end of ^var.

^var[1..2]

Find first and second characters of variables contents from the start.

^var]1..2[

Find first and second characters of variables contents from the end.

^var@^var1

If ^var=c and ^var1=abcd, the result in ^var is 3.  Finds position of character in ^var, from within ^var1.

^var%2

The variable ^var will contain remainder after its contents have been divided by 2.

^var#mm

Converts a numeric variable value from unit form to deci-micron form.

^var$mm

Converts a numeric variable value from deci-micron form to unit form.

^var{3

The variable ^var will be 3 characters in length by truncating or adding spaces from the start.

^var}3

The variable ^var will be 3 characters in length by truncating or adding spaces from the right.

The boxit command

The macro boxit can be used to display the value of a variable or a string on screen. This is useful when writing scripts to test if variable assignments are working correctly etc.  It is displayed on screen as a small box with the contents inside, for example:

Below is an example of using the boxit macro to display the contents of a variable:

^var=2 boxit ^var^

7
The variable 'var' is populated with the value 2.
9
The contents of 'var' are to be shown in a boxit.

The example above produces the following boxit:

The enter or escape keys will remove a boxit message from the screen.  For further information see boxit

Conditional tests with variables

The testing of variables is a fundamental part of scripting and consequently provides more scope for using and manipulating variables in Arbortext APP.  For example if a user has created a dialogue box with a tickbox then at some point the variable assigned to the tickbox will be tested to check its value.  For more information on scripting in Arbortext APP, go to 3B2 Scripting

The most common syntax for a conditional variable test is as follows:

 ? condition ^(item1) , ^(item2) action

Conditions

-

less than

+

more than

=

equal to

+-

not equal to

+=

greater than or equal to

-=

less than or equal to

A basic example would be:

 ?+^(^var1),^(^var2) trun 0

This is a test to see if ^var1 is greater than ^var2 and if true, issue the macro trun 0

Variables in conditional tests must include parentheses.

Below are some more examples:

Syntax

Explanation

?-^(^var),^(^var2) boxit

If ^var1 is less than ^var2

?+-^(^var1),^(^var2) boxit

If ^var1 is not equal to ^var2

?+=^(^var1),^(^var2) boxit

If ^var1 is greater than or equal to ^var2

?-=^(^var1),^(^var2) boxit

If ^var1 is less than or equal to ^var2

For further information on conditions and to see more tests see ?

The savevars command

Variables only retain their values until Arbortext APP has been shut down.  Variables that need to be preserved until a Arbortext APP session has started can be saved in a file and reread the next time they are needed.  The macro to save variables is savevars and it takes two parameters:

— a wildcard pattern for variable names

Wildcards can contain ? to match a single character or * to match all remaining characters, for example:

 savevars "c:all_vars","*" 6
 savevars "myvars","dave_*" 8
 savevars "d:\3d\types","type_?_var" 10

See savevars for more information.

The freevars command

There may be times when variables need to be reset during a Arbortext APP session.  The macro freevars can execute this and is particularly useful if the exact names of the variables are not known as a wildcard can be used like the savevars macro, for example:

 freevars 'var*'

This will clear all variables that begin with 'var'.

See freevars for more information.

Regular variables

Below are some of the characteristics of regular variables.

— There is presently a limit of 31 characters for regular variable names but this may be extended in the near future.

— Regular variables are shared amongst all open documents, therefore variables can be seen as global. If you assign a value to a regular variable that value will be available to all scripts, macros, expressions etc. for the remainder of the current Arbortext APP session.  Once you close Arbortext APP, all variables are lost.

Regular Arbortext APP variables are the same as Perl's scalar variables as shown below:

Arbortext APP

Perl

^name

$^name

^_

$^_

^_ar_3d

$^_ar_3d

For further information on Perl see the Perl in 3B2 chapter.

Indexed variables

Below are some of the characteristics of indexed variables.

— Each 'element' in the collection has an index number, starting from 0.  There is no longer any limit to the number of elements in an array.

— Negative array indices will start from the end of the array, with -1 being the last element.

— The index number itself can be any valid Arbortext APP expression, which depending on context, could access show string counters, variables and getvars.

Arbortext APP indexed variables are the same as Perl's array of scalar variables:

Arbortext APP

Perl

^array(5)

$^array[5]

^_(2)

$_[2]

^_job_info_(1)

$_job_info_[1]

Attribute variables

Attribute variables are used to access attributes applied to style and reference tags. The syntax is as follows:

 ^attr#

Attribute variables can only be used where this is applicable.  This will usually be during the formatting of text that contains tags with attributes for example:

Whenever an attribute variable is referenced, Arbortext APP will re-scan the relevant style tag markup to find the value of the attribute. Relevant markup will consist of all tags which resulted in the current format nesting level. It may also include tags (elements) that are higher in the hierarchy of a well-formed XML document. Relevant markup is re-scanned in reverse order so that only the most recent definition of an attribute will be found.  This allows for attributes to be overridden at any formatting or hierarchical level.  Below is an example of how a jump can be applied within a tag to identify the attribute value:

 <?}$^colour#><?-><?:red><?-><?:blue><?->
Attribute variables are read-only.  There is no way to assign a new value, other than by changing the markup. The option in the Names ▶ tab of the Document Preferences dialogue box (See the tdpref macro) allows this to be changed.

Like regular variable names, attribute names consist of alphabetic, underscore and numeric characters [a-zA-Z0-9_] but cannot start with a number.  However, it is common for SGML and XML files from external sources to have attribute names containing period (.) and hyphen (-) characters and sometimes other unusual characters. Unusual names syntax described later can be used to refer to these variables.  An older method of accessing such variables is also available for backwards compatibility. Any attribute name containing underscore characters which does match any of the rescanned markup will alternatively match markup which contains period or hyphen in place of the underscores, for example:

 <elem abc.def="1" abc-def="2" abc.xyz="3" abc_xyz="4">

attribute variables will match within <elem> as follows:

Variable

Matches

Value

Note

^'abc.def'#

abc.def 

1

^'abc-def'#

abc-def

2

^abc_def#

abc.def

1

1st possible alternative

^'abc.xyz'#

abc.xyz

3

^'abc-xyz'#

matches nothing

^abc_xyz#

abc_xyz

4

exact match takes priority

Attribute values within markup can be distinguished in four different ways:

— double quoted value

— single quoted value

Only the quoted variants are compatible with XML.  Single quoted attribute values are only recognised for non-XML streams if the relevant option is enabled in the XML ▶ tab of the Document Preferences dialogue box (see tdpref), for example:

Type of Attribute Value

Example

<elem no_border>

no value

<elem border=5>

unquoted value

<elem name="John Smith">

double-quoted value

<elem title="3B2 Guru">

single-quoted value

A Arbortext APP extension function allows double or single quotes to be embedded within their own value types if they are paired, for example:
 <elem owner='John''s PC'>

The single quote in John's is paired or 'doubled-up' so that Arbortext APP can undersatnd and process the information.

 <elem macro ="trun ""autoexec""">

The double quotes around autoexec are paired or 'doubled-up' so that Arbortext APP can undersatnd and process the information.

10 

Special attribute variables

There are three special attribute variables identified by their names of one, two or three underscores as shown below:

^_#

The name of the current tag.

^__#

The value assigned to the current tag.

^___#

The exact name of the current tag.

Below are some examples of special attribute variables:

Markup

^_#

^__#

^___#

<body>

body

red

<body

&ref;

ref

&ref

<body=5>

body

5

<body

&ref=9;

ref

9

&ref

Note that the last two markup examples are Arbortext APP specific extensions that are not available in SGML or XML,.  In fact, references (entities) cannot have attribute values at all in SGML/XML.  In Arbortext APP, the style or reference name itself can be referenced as an attribute variable if it has a value.

The <?attr> command can be used to adjust Arbortext APP's idea of the current formatting position in each currently nested style tag.  It is this formatting position which determines the relevant markup that is re-scanned, so <?attr> can also be used to scan other markup tags for attributes.

11 

Indexed attributes

The syntax for an indexed attribute variable is:

^attr#(0)

In the above example 'attr' is the attribute.

For example, if:

 <elem girl=36,24,35>

Then attribute variables will match within <elem> as follows:

^girl#(0)

36,24,35

same as ^girl#

^girl#(1)

36

^girl#(2)

24

^girl#(3)

35

As can be seen from that example, index 0 refers to the whole attribute value, as if no index were present at all.  If any value contains spaces, it can be placed in quotes. Another markup/values example is illustrated below:

If:

 <elem job="J Smith", 24,"3B2 Technician">

Then:

^job#(0)

J Smith same as ^job#

^job#(1)

J Smith

^job#(2)

24

^job#(3)

Arbortext APP Technician

Note that for ^job#(0), Arbortext APP stops at the first quote within the value and disregards the rest.

The special ^__# variable can also be indexed.  In this way, styles can be renamed without needing internal changes.

For example, if:

 <setfont=10,12, "Times New Roman">

Then:

^__#(0)

10,12,Times New Roman

^__#(1)

10

^__#(2)

12

^__#(3)

Times New Roman

12 

Unusual names

Sometimes it is necessary to refer to variables or attribute variables which do not conform to Arbortext APP's standard naming convention. This is particularly true when data is imported from external sources, or if Perl's special punctuation variables need to be referred to. The way to access such variables is simply to enclose the name part of the entire variable in single quotes, for example:

 ^'name.ex'

Arbortext APP does not interpret or evaluate anything inside the quotes, but simply passes the name (without the quotes) to the relevant system (such as Perl).

13 

Stream variables

Arbortext APP text streams can be accessed as though they were variables by using double quotes around the stream name, for example:

 ^"stream"

— Information can also be passed to stream variables.  If the stream does not exist, it will be created.

Some useful examples are shown (as macros) in the table below:

Syntax

Description

@^"address"^

Insert "address" stream at cursor position.

^"err_log"=

Create or clear "err_log" stream.

^"dest"=^"source"

Copy a stream to another.

14 

Miniscripts - ^"*script"

— Miniscripts can also be Perl scripts. For more information see Perl in 3B2

— Miniscript users need not be concerned with which script system the miniscript is executing.  Perl miniscripts cannot use any call-back features to Arbortext APP (such as running macros or accessing streams as files).

Below is an example of a Arbortext APP miniscript in a stream called "weekday":

 ^weekday?$210^(v01037) 1
 ^weekday[1..3] 2

The following example is a Perl equivalent, also in a stream called "weekday":

 return substr gmtime, 0, 3;

Both of the above scripts will result in the regular variable ^weekday containing the first three characters of today's day name.  That same variable will be used as the result of the miniscript reference.  So, for either script, the show string command shown below will result in the current day name being shown:

 <?show $^"*weekday">

A special variation on the name allows Arbortext APP miniscripts to be sourced from a Arbortext APP string instead of a document stream.  One advantage of this is that miniscripts can be written that are available to any open document or even when no documents are open.

The syntax for special names for string access to miniscripts is:

 ^"*_str_12345_"

The above syntax runs miniscript in string 12345.

In this case, a regular Arbortext APP variable, ^_str_12345_ would have to be populated with any desired return value.

For more information see the 3B2 Scripting chapter.

15 

System variables

System variables enable the user to extract information from Arbortext APP about the setup and/or state of the current document. Some basic examples of system varaibles include: the full document name, the number of pages and the position in the text stream. For more information see the System Variables chapter.

System variables standardise access to getvars from scripts, showstrings and expressions. System variables are also referred to as getvars For more information see getvar

There are several variations of syntax that can be used to use system variables. System variables or getvars can be accessed as though they were variables.  To some extent, this is a convenience feature which makes the getvar macro somewhat redundant.

However, getvar is still useful for increased readability and to access several consecutive variables at the same time.  This is usually much faster, but also more reliable for things like the 'time' getvars which could change between getvar calls.

Below are some examples of different kinds of system variable use:

Verbose getvar usage

Concise, consistent usage

getvar 11500, "filename"

^filename=^11500

<?show $11500v>

<?show $^11500>

(getvar 1500, "filename")

(@^filename^)

16 

Indexed system variables

Many of the new system variables or getvars require a parameter to provide the context of the request.  This is supplied as the index of the getvar variable. Indices can be any valid Arbortext APP expression and an index of (0) is the same as having no index at all. The syntax for indexed system variables is as follows:

 ^12345(n)

In the above example syntax ^12345 is a getvar variable and the (n) is the index of the getvar variable.

Below are some examples of indexed system variables:

^11830(0) name of current stream.

^11830(1) name of parent stream/style.

^11830(2) name of grandparent stream/style.

Note that the ability to index getvar numbers is system-wide and not specific to variable syntax. Getvars are currently read-only.

17 

Named Getvars

Getvars also have an alternative method of access via a name. Using this method results in syntax that is easier to read. For example, the three different versions of syntax below all achieve the same result of retrieving the document filename, but the third version is much easier to identify and understand:

 ^name=^11500 4
 name=^[getvar 11500] 6
 name=^[document.filename] 8
Below are the conventions for named getvars:

— If a parameter is present then it is used as the index of the getvar.

The system variable examples below all return the same value:

 ^name=^[getvar 31000(123)] 9
 ^name=^[sys.string(123)] 11
 ^name=^[sys.string 123] 13

Alternatively the string function can be used more efficiently like this:

 ^name=^[string 123]

The getvar macro also understands the new names, for example:

 getvar 11500, "name"

The above example can also be specified as:

 getvar document.filename, "name"
It is helpful to note that named getvars are similar to Function Variables. For more information see Function Variables

18 

Expressions

Any valid Arbortext APP expression can be used as a variable by enclosing it in parentheses, such as:

 ^(expr)

In the above example 'expr' is the expression. For example, an expression could be ^(^var1+^var2)

Arbortext APP expressions always return a number, never strings.

If the expression variable is used in a context where a string is required then the number will be converted to a string, for example, the insert macro can only take a string. So, in the following example the number 304 is automatically converted to a string:

 @ ^(304)

Consider the following example. The variable assignments will result in '0' being assigned to ^test as will any other variable which contains a string that cannot be converted to a valid number.

 ^name=Dave 1
 ^test=^(^name) 2

This conversion to a number can be useful in a Arbortext APP condition macro, for example:

 ?+^value,^min}label

The above syntax is comparing two strings (a string compare).

 ?+^(^value),^(^min)  }label

Due to the expression brackets and because Arbortext APP expressions always return number, the above syntax is is comparing two numbers (a numeric compare).

19 

Indirect variables

Arbortext APP's indirect variables are identified by any string of characters within curly braces, for example:

 ^{string}

The curly braces force Arbortext APP to evaluate the string using standard Arbortext APP variable interpolation and the result string is then re-parsed as the full variable name, except for the leading ^ character.  The small script below demonstrates this:

Syntax

Explanation

^{name}=attr

same as ^name=attr

^{idx}=(5)

same as ^idx=(5)

^att=^{^{name}#^idx}

sets ^att to ^attr#(5)

^style_lev={11830(^level)}

^level=1

^parent=^{^style_lev}

parent style (level1)

^level=2

^parent=^{^style_lev}

grandparent style (level 2)

^ctr_size=23

^size=^(x(^ctr_size))

get x(23) (old method)

^ctr_size=(x(23))

^size=^{^ctr_size}

get x(23) (better method)

^att_name=first.name

^name=^{'^att_name'#}

access ^'first.name'#

^att_name=last.name

^name=^{'^att_name'#}

access ^'last.name'#

In the third line of the example script above, note the nested curly braces around {name} that prevent the following # from being taken as part of the wrong variable name, so that ^name is substituted, and not ^name#.

20 

Function Variables

Function variables enable you to call functions by name.  An example of the syntax is given below:

 ^[funcname parameter(s)]

— All of the previous variable kinds now have an equivalent function to do the same.

21 

Functions to access variables

Function

Readable method

Equivalent to

var

^[var name]

^name

attr

^[attr name]

^name#

stream

^[stream name]

^"name"

mini

^[mini name]

^"*name"

getvar

^[getvar 01234]

^01234

indirect

^[indirect xxx^yyy]

^{xxx ^yyy}

expr

^[expr 1+2-a+b]

^(1+2-a+b)

show

^[show +a$a]

^?"+a$a"

xpath

^[xpath count(a)]

^!"count(a)"

xpointer

^[xpointer /1/2]

^!!"/1/2"

attrloop

^[attrloop 2]

^@(2)

tokloop

^[tokloop 2]

^@@(2)

measure

^[measure12pt]

^#'12pt'

Function

Example

Description

prop

^[prop th]

Get current <?th> string.

propval

^[propval th]

Get current <?th> value.

length

^[length string]

Get length of string.

lookup

^[lookup 123,name]

Lookup name in group 123.

resolve

^[resolve {area}xxx]

Resolve filename with areas.

string

^[string 123]

String 123 from strings file.

perl

^[perl perl_code]

Evaluate a Perl expression.

rgb

^[rgb colour]

Colour as {r,g,b}

rgb_r

^[rgb_r colour]

Red component of colour.

rgb_g

^[rgb_g colour]

Green component of colour.

rgb_b

^[rgb_b colour]

Blue component of colour.

cmyk

^[cmyk colour]

Colour as {c,m,y,k}

cmyk_c

^[cmyk_c colour]

Cyan component of colour.

cmyk_m

^[cmyk_m colour]

Magenta component of colour.

cmyk_y

^[cmyk_y colour]

Yellow component of colour.

cmyk_k

^[cmyk_k colour]

Black component of colour.

hsb

^[hsb colour]

Colour as Arbortext APP hsb.

hsb_h

^[hsb_h colour]

Hue component of colour.

hsb_s

^[hsb_s colour]

Saturation component of colour.

hsb_b

^[hsb_b colour]

Brightness component of colour.

lump_expand

^[lump_expand lump]

Expand lump to processing instructions.

23 

Function Parameters

Most functions will take exactly one parameter and that consists of everything up to the next unmatched ] character. Matched [ and ] pairs may appear in the parameter.

Embedded strings in single or double quotes may contain ] characters without terminating the parameter, for example:

 ^[length abc [123]def]  =11 5
 ^[length abc"]"def]       =9 7

Rarely the parameter may consist of multiple sub-parameters.  For example, the ^[prop xxx] function's parameter may be split when requesting font properties as shown below:

 ^[prop tf,l]

l=long name

 ^[prop tf,s]

s=short name

It is still important to consider the parameter as one parameter that may be subdivided depending on the function.  The reason is because one single Arbortext APP variable can be used as the parameter, for example:

 ^info = th 13
 ^p=^[prop ^info] 15

More than one variable is not presently allowed

 ^info=tf 16
 ^type=s 18
 ^p=^[prop ^info,^type] 20

And if there is a variable, it must not have sub parameters

 ^p=^[prop ^info,s]

But it can be done by using the curly braces which evaluates all the variables first before the function is processed:

 ^p=^{[prop ^info, ^type]}

Alternatively use the indirect function which effectively is the equivalent of ^{....}

 ^p=^[indirect [prop ^info, ^type]]

Or prepare the compound parameter before a syntax similar to the following examples:

 ^info=tf,s 26
 ^p=^[prop ^info] 28

24 

Further Information and References

For more information on the ^ macro see ^

For more information on scripting in Arbortext APP, go to 3B2 Scripting

For more information on system variables see the System Variables chapter.

For further information on Perl see the Perl in 3B2 chapter.

For further information on Show strings see the Show strings chapter.


Document created on 04-Jan-2002 (revision 0)