5.2.3 Variables and translators
Having the basic navigation and output in place for our
example generator, we can now extend the original requirements by reporting the
number of time units provided by each Display into our spec sheet. To do this,
we need a count variable, a new loop and some arithmetic. With the modifications
our example generator will appear as follows:
01 SpecSheet()
02 _translators()
03 'SpecSheet for ' id newline newline
04 foreach >Watch
05 { 'Watch: ' id newline
06 do .Display
07 { ' Number of time units shown: '
08 @count = '0'
09 do :UnitZones { @count++%null }
10 @count newline
11 ' Buttons: '
12 do :Buttons { id ' ' }
13 newline
14 }
15 do .LogicalWatch
16 { ' Apps: '
17 do decompositions
18 { foreach .State [Watch] { id ' ' } }
19 newline
20 }
21 newline
22 }
The code that counts and outputs the number of time
units can be found in lines 7 – 10. After outputting the header text in
line 7, we define a local variable called
count and initialize it to zero in line
8. Like everything in MERL, this is really just a string rather than a number,
but we can interpret it as a number when we want. The
@ prefix means this is a local
variable, whose value is visible only throughout this generator; a global
variable is prefixed with a $ and is
visible throughout all generators in a given execution run.
In line 9, we loop through the elements in the UnitZone
property collection. However, instead of retrieving and outputting any property
values we will now just simply increment our counter on each iteration. The
command for this:
@count++%null
takes the
@count variable, increases it by one
and — instead of printing the old value of
$count — throws away the output
with the %null translator. This output
reformatter comes from a set of standard translators that come as part of
MetaEdit+’s generator development environment. In order to be able to use
these translators, a subgenerator defining them is called at the beginning of
the generator in line 2 (we will see more translators in action shortly). After
all the iterations the variable @count
will then hold the number of available time unit slots. This number will then be
output (followed by a newline command)
in line 10.
Having completed the basic functionality of our example
generator, we will now see how to refine it further, starting with outputting to
a file.