Types de variables

Comme expliqué dans la section générale, les variables n’ont pas un type assigné lors de la déclaration mais lors de l’assignation ou lors de l’utilisation.

Cette page explique les différents types de variable et leur utilisation.

Types numériques

Le langage de script dispose des types numériques entiers et des nombres à virgule flottante.

num = 1
num += 2        // Adding 2, num = 3
num -= 2        // Substract 2, num = 1
num *= 2        // Multiply by 2, num = 2
num /= 2        // Divide by 2, num = 1

num = 2^3       // power function, num = 8
num = abs(-3)   // absolute value, num = 3

a = 1/3         // float assignation, a = 0.333...
a *= 2.1        // multiply by 2.1, a = 0.6999...

b = sqrt(2)     // square root, b = 1.4142...
c = 2^0.5       // square root equivalent, c = 1.4142

x = -2.2
d = (x-4)*(x+3) // d = -4.96

y = 10
z = 4
e = y/z         // e = 2.5

Types caractère

Le langage de script possède un certain nombre de méthodes d’aide pour la manipulation des chaînes de caractères.

name = "Durand"                    // String declaration
surname = "Michel"

fullname = name :: " " :: surname  // String concatenation

initials = name[1] :: surname[1]   // "D" :: "M" = "DM"
part = name[3:5]                   // "ran"
part = name[1:999]                 // "Durand"
part = name[999:1]                 // "dnaruD"

len = name.length                    // length of the chain
pos = name.find("ch")                // position of the substring in the string

Note that the strings are indexed starting at 1 and not 0.

Two commands are available to force the interpreter to handle variables as numerics or as strings : toFloat and toString:

str1 = "23.0"
flo1 = str1 (toFloat)       // flo1 = 23
flo1 *= 2                   // flo1 = 46
str1 = flo1 (toString)      // str1 = "46"

Date

The script language has a date type which can be easily used for computations.

date1 = 22/03/1985
date2 = todayDate

// date arithmetics
date3 = date1 + 2                   // date3 = 24/03/1965 ( + 2 days)
delta1 = date2 - date1              // number of days between the two dates
delta2 = (date2 - date1)/365        // approx number of years between the two dates
delta3 = yearsBetween(date2, date1) // number of years between the two dates

// date conditions
if (date1 < date2) ...
if (date2 > date1) ...
if (date1 = date2) ...

As with the string conversion, the dates can be converted using toDate and toString:

str1 = "22/03/1985"
str2 = "01/03/2002"

date1 = str1 (toDate)
date2 = str2 (toDate)

dlta1 = yearsBetween(date1, date2) (toString) // = "16"
text1 = "There are " :: dlta1 :: " years between the two given dates."

dlta2 = date2 - date1 (toString)              // = "LARGE_NUMBER"
text2 = "And more precisely " :: dlta2 :: " days."