Naturally variables can be numbers and in fact this is the normal variable. A number variable has a name that must start with a letter. Again the name should represent the contents.
Be careful though. Our numbers are integers. That means whole numbers and not fractions. If you want floating point numbers, 7.385 style, we'll deal with that later.
We can handle big numbers being from +2147483648 to –2147483649
You assign values to numeric variables just as you do to strings as
x = 5
y = 10
Be careful. The lines above do not say something like "x is equal to five" but "take a five and put it in the place we call x". They are referred to as assignments. You can change variables at any time and, in fact, the script line x = x+5 which is abject rubbish in algebra just tells script to assign x a new value that is the current value of x with five added to it.
You can do more sums with numbers and we do all the usual things like add, subtract, multiply and divide. We use + for add, - for subtract, * for multiply because x looks like a letter and we would treat it as a variable and / for divide. We also obey the rules about brackets that you leaned in algebra at school so z = 4*3+5*4 gives 32 not any of the other combinations and if you don’t want that you use brackets like 4*(3+5)*4 to get 128.
If you want to put a number into a string to display it just add it on so "value ="+var would represent a string like "value = 5" if var happened to contain 5. Be careful. If the something+something_else starts with a string it is all a string so given var1=5 and var2=6
"values are "+var1+" and "+var2 |
values are 5 and 6 |
"values are "+var1+""+var2 |
values are 56 |
"value is "+var1+var2 |
value is 11 |
""+var1 |
5 |
As you see from the above examples + for numbers works before + for strings so var1+var2 was calculated and that was used.
A very important number is returned by a function time( ). This returns the time as the number of seconds since the start of 1st Jan 1980 and when I write this it returns 637582544 so we have a good few years to go before 19th January 2048 when we suddenly hit the maximum integer we can handle. Please make a note in your diary to call our office in the summer of 2047 to pick up the patch to fix this.
Since numbers like this are hardly useful we provide a function $time( ) that converts a time in seconds into a text time: try a script that says
message("It is now "+$time( ))
Which, when I first started writing this manual, displayed
Because you have time in seconds you can time events in scripts. eg:
t = time( )
message("It is now time "+$time(t)+"$nPlease wait a few seconds")
message("It is now time "+$time( )+"$nYou waited "+time( )-t+" seconds")
This records the current time in seconds in the variable t. That is t is the name of the place we put that number. It then displays a message box with the current time and the instruction to wait. Notice the use of $n as a <new-line> code to make the text more readable. After you have waited and then clicked the OK button the script continues to calculate the new current time and subtracting the old time from it. So you get the delay, in seconds, between the first line of the script and the third which is displayed.
There are lots of things you can put into strings but not all of them are useful in message boxes. There is a full list in the appendixes but notice that if you want the $ character you just use two.
If you need more resolution than seconds you can use the ticks( ) function that returns the time since windows was started on that PC (since the last power up or reboot) in thousandths of a second. However this counter runs out every 24 days and starts again so if you subtract two tick counts and get a negative number add 2147483648 to it to get the right answer. Beware however, using ticks some of the delay you are timing is the time taken for the script to run. If a script is waiting for something to happen it lets Windows go off and do other things and it will not run again until Windows gives it time to run so it is probably good for tenths of a second, so-so for hundredths but dubious for thousandths.