Previous section To contents

1.7 Data Types

As you saw in our first examples we have to indicate the type of value returned by a function or contained in a variable. We used integers (int), strings (string), and arrays (with the * notation). The others are mapping, mixed, void, float, multiset, function, object and program. Neither mixed nor void are really types, void signifies that no value should be returned and mixed that the return value can be of any type, or that the variable can contain any type of value. Function, object and program are all types related to object orientation. We will not discuss the last three in any great detail here,
Int
The integer type stores a signed integer. It is 32 bit or 64 depending on architecture.
Float
This variable type stores a floating point number.
Array
Arrays are basically a place to store a number of other values. Arrays in Pike are allocated blocks of values. They are dynamically allocated and do not need to be declared as in C. The values in the array can be set when creating the array like this:
arr=({1,2,3});
Or, if you have already created an array, you can change the values in the array like this:
arr [ ind ] = data;
This sets entry number ind in the array arr to data. ind must be an integer. The first index of an array is 0 (zero). A negative index will count from the end of the array rather than from the beginning, -1 being the last element. To declare that a variable is an array we simply type array in front of the variable name we want:
array i;
We can also declare several array variables on the same line:
array i, j;
If we want to specify that the variable should hold an array of strings, we would write:
array (string) i;
String
A string contains a sequence of characters, a text, i.e. a word, a sentence or a book. Note that this is not simply the letters A to Z; special characters, null characters, newlines and so on can all be stored in a string. Any 8-bit character is allowed. String is a basic type in Pike, it is not an array of char like it is in C. This means that you cannot assign new values to individual characters in a string. Also, all strings are "shared", i.e. if the same string is used in several places, it will be stored in memory only once. When writing a string in a program, you enclose it in double quotes. To write special characters you need to use the following syntax:
\nnewline
\rcarriage return
\ttab
\bbackspace
\"" (quotation character)
\\\ (literal backslash)
Mapping
A mapping is basically an array that can be indexed on any type, not just integers. It can also be seen as a way of linking data (usually strings) together. It consists of a lot of index-data pairs which are linked together in such a way that map[index1] returns data1. A mapping can be created in a way similar to arrays:
mapping(string:string) map=(["five":"good", "ten":"excellent"]);
You can also set that data by writing map["five"]="good". If you try to set an index in a mapping that isn't already present in the mapping it will be added as well.
Multiset
A multiset is basically a mapping without data values. When referring to an index in the multiset a 1 (one) will be returned if the index is present and 0 (zero) otherwise.

Previous section To contents