HSL variables and data types
Contents |
Variables
A variable can hold a value of any Data Type. A variable must be defined be used. A variable represent the exact value and type it was assigned. A variable must start with a $ sign. It may only contain A-Z, a-z and numbers, but it may not start with a number ($[a-zA-Z]+[a-zA-Z0-9]*).
Code example(s)
echo $variable; // Incorrect, not defined before used
$variable = "Hello World";
echo $variable; // Correct
A variable can be changed by simple assign it a new value.
$variable = "Hello";
echo $variable; // prints "Hello"
$variable = "Hello World";
echo $variable; // prints "Hello World"
Variables may be used inline in Strings, read more about this in the String chapter.
List of data types
In HSL there are three main data types, these are numbers, strings and arrays. Each data type may be converted using built-in function and in some cases automatic conversion using Arithmetic operators.
Number
A number is a floating point value, with at most 5 decimals. When automatically converted to a string it will be rounded up to 2 decimals if its not equal a integer without rounding. If you want to print a number with higher accuracy (more decimals), you should use the core function round, which lets use print up to all five decimals.
Code example(s)
echo 1.0;
echo 1.0000;
echo 1.12345;
echo round(1.12345,5);
Expected result
1 1 1.12 1.12345
Boolean
In HSL a value of 0 represent "false" and 1 represent "true". There are two constants that represent these values.
Code example(s)
echo true;
echo false;
Expected result
1 0
String
A string is a set of characters, its boundaries are the " character.
Code example(s)
echo "Hello World";
Expected result
Hello World
The index operator [] may be used on strings to return a certain character.
$variable = "Hello";
echo $variable[4];
Expected result
o
In order to add non-ASCII or special characters to a string, you may add any ASCII charter using \x##, where ## is representing it's hexadecimal value.
echo "Hello\x0AWorld";
Expected result
Hello World
Inline variables
You may use Variables inline in Strings instead of concatenate them using the + operator. In order to find a Variable in a String HSL match the name of the variable starting with $ until it finds a character which is not ([a-zA-Z]+[a-zA-Z0-9]*).
$fruit = "Apples";
echo "I like $fruit"; // prints "I like Apples"
$fruit = "Apple";
echo "I like $fruits"; // Error (it will try to resolve $fruits which is not a defined variable)
$fruit = "Apple";
echo "I like $fruit(s)"; // prints "I like Apple(s)" (it will match $fruit since "(" is no allowed in a variable name)
Regular expressions
A regular expression is a normal string until it is used as a regular expression eg using the =~ operator. Our implementation is "Perl Compatible" (PCRE) so if you want to learn more about regular expressions the http://perldoc.perl.org/perlre.html manual may be a good place to start.
If you want to explicit mark the beginning or end of a filter you should use ^ for beginning and $ for the end. Even thou $ is a start of a inline variable it will not be resolved to a variable since it is not followed by a valid Variable name.
If you want to escape a part of a Regular Expressions you must escape the \ using \\ which to the Regular Expression Engine will look like a single \
Pattern modifiers
If you want to change the behaviour of the pattern enginge you may use pattern modifiers, they have the capability to make the match caseless and activate UTF-8 support (where one UTF-8 characters may be matched using only one dot) etc. They are activated by encapsulate the pattern using /regular_expression/modifiers. The regular_expression part should be a normal regular expression, and the modifiers should be one or many of:
| Modifier | Internal Define | Description |
|---|---|---|
| i | PCRE_CASELESS | This will result in a case-insensitive match, where abc matches AbC. |
| m | PCRE_MULTILINE | See perl documnentation |
| u | PCRE_UTF8 | UTF-8 support, will make an UTF-8 character build up with multiple ascii characters be mached by a single . |
| s | PCRE_DOTALL | See perl documnentation |
| x | PCRE_EXTENDED | See perl documnentation |
| U | PCRE_UNGREEDY | See perl documnentation |
| X | PCRE_EXTRA | See perl documnentation |
Unless you are familiar with regular expressions, only the i and the u modifiers may be interesting.
| String | Filter | Result |
|---|---|---|
dummy@example.org |
^dummy@example\\.org$ |
Match (this is the correct way to match this mail address) |
dummy@example#org |
^dummy@example.org$ |
Match (the dot is not escaped, it will match any character eg. #) |
DUMMY@EXAMPLE.ORG |
/^dummy@example\\.org$/i |
Match (see documentation about the i modifier.) |
Array
An array is a list of values, an array can contain numbers, strings and other arrays. Arrays are created using the core function array().
Code example(s)
echo array(1,"Apple",array("Banana","Orange"));
Expected result
(0=>1,1=>"Apple",2=>(0=>"Banana",1=>"Orange"))
Accessing an single element in an array can be done by using the index operator "[expression]", this operator can only be used on variables. The type of the value return is the same type as of the value in the array. Arrays can be used as associative arrays by using an index of any data type (especially one that is not a number). As mentioned above indexes can be of any data type, but for your convenience there is some conversion between data types to simplify the use of non associative arrays.
Here is the conversion table for the index value.
$array[5] => $array[5]
$array[5.0] => $array[5]
$array["5"] => $array[5]
$array["5.0"] => $array["5.0"]
$array[array()] => $array[array()] // Indexes can be of any data type, even arrays.
If the array is modified it will not reorder its elements, if one element is removed there will be a gap in the non associative arrays index. A "work around" (this language feature) is to create a new array, and merge the array against it.
$t=array()+$t;
This works because the it will merge $t with an empty array(), and as described in the Core/Operators chapter non associative values will be reordered when merged.
The first element in an array starts normally by index zero (if not modified, see above). That said, $list[1] equals the second element.
Code example(s)
echo array(1,"Apple",array("Banana","Orange"))[0]; // Incorrect, array() is not a variable
$list = array(1,"Apple",array("Banana","Orange"));
echo $list[0]; // Correct will return 0 as a number
echo $list[1]; // Correct will return "Apple" as a string
echo $list[2]; // Correct will return ("Banana","Orange") as an array
More code example(s)
$map = array("fruits"=>array("banana","apple","orange"),"soft drinks"=>array("coca cola","fanta","sprite"));
echo $map["soft drinks"];
echo $map["soft drinks"][2];
Expected result
(0=>"coca cola",1=>"fanta",2=>"sprite") "sprite"
Data type conversions
In some cases you might want to convert data types to another data type. There are some function and ways to do it described in these examples.
Convert to number
number(array("1","2","3")); // returns the number of elements in the array
number("1.12345"); // 1.12345
Convert to string
string(array(1,"Banana")); // Returns "(1,"Banana")"
string(1.12345); // "1.12"
Convert to array
array("apple");
array(5);
array(5,"apple");