Variables are reserved memory locations to store values. It means you reserve some space in the memory by create a variable.
Based on the data type of a variable, the compiler/interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to the variables, you can store integers, decimals, boolean or characters in these variables.
Spla variables need declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the right of the = operator is the value stored in the variable. For example −
int intvar = 500; !! An integer assignment flt floatvar = 6000.5; !! A floating point str strvar = 'Dibakar Nandi'; !! A string << '\n'+intvar; << '\n'+floatvar; << '\n'+strvar;
Here, 500, 6000.5 and 'Dibakar Nandi' are the values assigned to intvar, floatvar, and strvar variables, respectively. This produces the following result −
500 6000.5 Dibakar Nandi
Here, two integer objects with values 1 and 2 are assigned to the variables a and b respectively, and one string object with the value 'Dibakar Nandi' is assigned to the variable c.
The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Spla has various standard data types that are used to define the operations possible on them and the storage method for each of them.
Spla has five standard data types −
Number data types store numeric values. Number objects are created when you assign a value to them. For example −
int var1 = 1; int var2 = 10;
Spla supports three different numerical types −
All integers in Spla are represented as long integers. Hence, there is no separate number type as long.
Here are some examples of numbers −
| int | float |
|---|---|
| 10 | 0.0 |
| 100 | 15.20 |
| -786 | -21.9 |
| 080 | 32.3+e18 |
| -0490 | -90. |
| -0x260 | -32.54e100 |
| 0x69 | 70.2-E12 |
Strings in Spla are identified as a contiguous set of characters represented in the single quotation marks. Spla allows either pair of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 to the end.
The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. For example −
str strvar= 'Hello World!'; << '\n'+strvar; !! Prints complete string << '\n'+strvar[0]; !! Prints first character of the string << '\n'+strvar[2:5]; !! Prints characters starting from 3rd to 5th << '\n'+strvar[2:]; !! Prints string starting from 3rd character << '\n'+strvar* 2; !! Prints string two times << '\n'+strvar+ 'TEST'; !! Prints concatenated string
This will produce the following result −
Hello World! H llo llo World! Hello World!Hello World! Hello World!TEST
Spla required to diclare using square brackets ([]). Spla array can be int or str type.
str strarray[] = [ 'str1', 'str2' , 'str3', 'str4', 'str5' ]; str strarray2[2] ; strarray2[0] = 'str6'; strarray2[1] = 'str7'; << '\n'+strarray; !! Prints complete array << '\n'+strarray[0]; !! Prints first element of the array << '\n'+strarray[1:2]; !! Prints elements starting from 2nd till 3rd << '\n'+strarray[2:]; !! Prints elements starting from 3rd element << '\n'+strarray2 * 2; !! Prints list two times << '\n'+strarray + strarray2; !! Prints concatenated lists
This produces the following result −
[ 'str1', 'str2' , 'str3', 'str4', 'str5' ] str1 ['str2' , 'str3'] [ 'str3', 'str4', 'str5'] ['str6', 'str7' , 'str6', 'str7' ] ['str1', 'str2' , 'str3', 'str4', 'str5', 'str6', 'str7' ]
Integer array
int intarray[] = [ 1, 2 , 3, 4, 5 ]; int intarray2[2] ; intarray2[0] = 6; intarray2[1] = 7; << '\n'+intarray; !! Prints complete array << '\n'+intarray[0]; !! Prints first element of the array << '\n'+intarray[1:2]; !! Prints elements starting from 2nd till 3rd << '\n'+intarray[2:]; !! Prints elements starting from 3rd element << '\n'+intarray2 * 2; !! Prints list two times << '\n'+intarray + intarray2; !! Prints concatenated lists
This produces the following result −
[ 1, 2 , 3, 4, 5 ] 1 [ 6 , 7 ] [ 3, 4, 5 ] [ 6 , 7 , 6 , 7 ] [ 1, 2 , 3, 4, 5 , 6 , 7 ]
Spla required to diclare using tww square brackets ([][]). Spla array can be int or str type.
str strarray[][] = [[ 'str1', 'str2' ],[ 'str3', 'str4'],[ 'str5', 'str6' ]]; str strarray2[2][2] ; strarray2[0][0] ='str7'; strarray2[0][1] ='str8'; strarray2[1][0] ='str9'; strarray2[1][1] ='str10'; << '\n'+strarray; !! Prints complete array << '\n'+strarray2; !! Prints complete array << '\n'+strarray[0][0]; !! Prints first element of the array << '\n'+strarray[1:2]; !! Prints elements starting from 2nd till 3rd << '\n'+strarray[2:]; !! Prints elements starting from 3rd element << '\n'+strarray2 * 2; !! Prints list two times << '\n'+strarray + strarray2; !! Prints concatenated lists
This produces the following result −
[[ 'str1', 'str2' ],[ 'str3', 'str4'],[ 'str5', 'str6' ]] [[ 'str7', 'str8' ],[ 'str9', 'str10']] str1 [[ 'str3', 'str4'],[ 'str5', 'str6' ]] [[ 'str5', 'str6' ]] [[ 'str7', 'str8' ],[ 'str9', 'str10'],[ 'str7', 'str8' ],[ 'str9', 'str10'] ] [[ 'str1', 'str2' ],[ 'str3', 'str4'],[ 'str5', 'str6' ],[ 'str7', 'str8' ],[ 'str9', 'str10'] ]
Two dimentional Integer array
int intarray[][] = [[ 1, 2 ],[ 3, 4],[ 5, 6 ]]; int intarray2[2][2] ; intarray2[0][0] =7; intarray2[0][1] =8; intarray2[1][0] =9; intarray2[1][1] =10; << '\n'+intarray; !! Prints complete array << '\n'+intarray2; !! Prints complete array << '\n'+intarray[0][0]; !! Prints first element of the array << '\n'+intarray[1:2]; !! Prints elements starting from 2nd till 3rd << '\n'+intarray[2:]; !! Prints elements starting from 3rd element << '\n'+intarray2 * 2; !! Prints list two times << '\n'+intarray + intarray2; !! Prints concatenated lists
This produces the following result −
[[ 1, 2 ],[ 3, 4],[ 5, 6 ]] [[ 7, 8 ],[ 9, 10]] 1 [[ 3, 4],[ 5, 6 ]] [[ 5, 6 ]] [[ 7, 8 ],[ 9, 10],[ 7, 8 ],[ 9, 10]] [[ 1, 2 ],[ 3, 4],[ 5, 6 ],[ 7, 8 ],[ 9, 10]]
Sometimes, you may need to perform conversions between the built-in types. To convert between types, you simply use the type-names as a function.
There are several built-in functions to perform conversion from one data type to another. These functions return a new object representing the converted value.
| Sr.No. | Function & Description |
|---|---|
| 1 | int(x) Converts x to an integer. The base specifies the base if x is a string. |
| 2 | flt(x) Converts x to a floating-point number. |
| 3 | str(x) Converts object x to a string representation. |