~~Title: Counting via Global Variables~~
{{tag>guides}}
==== How-to: Simple Counting using Global Variables ====
It is always recommended to use testnet/sandbox first!
#increment and check counter (if the variable doesn't exist it is basically zero, becoming 1 after the increment)
inc=$counter
if=($counter > 5) set=$counter:1
if=($counter = 1) do=count1
if=($counter = 2) do=count2
if=($counter = 3) do=count3
if=($counter = 4) do=count4
if=($counter = 5) do=count5
exit
:count1
echo=~~codedoc:clean:"First call!"~~
exit
:count2
echo=~~codedoc:clean:"Second call!"~~
exit
:count3
echo=~~codedoc:clean:"Third call!"~~
exit
:count4
echo=~~codedoc:clean:"Fourth call!"~~
exit
:count5
echo=~~codedoc:clean:"Fifth call - starting over!"~~
exit
A more **simplified** version would look like this – if you just need the actual number:
#increment and check counter (if the variable doesn't exist it is basically zero, becoming 1 after the increment)
inc=$counter
if=($counter > 5) set=$counter:1
echo=~~codedoc:clean:"This is call number {$counter} of 5!"~~
**Note:** Global variables are **shared** across **all** alerts so make sure to either chose unique names or add preprocessor placeholders like ''[id]'' or ''[name]'' to the variable name, eg. ''inc=$count[id]''!
=== Attention ===
Always double check that you are using the right syntax/alert!