An alternative way to specify a Conversion is using the "conv" parameter:
display "The time now is ";
display "{sys%time}" conv="strftime:%d/%m/%Y (%a)";
display ".
";
The "conv" parameter applies to the whole string, so the following does NOT work because the Conversion will take the whole sentence as input:
// WRONG
display "The time now is {sys%time}.
" conv="strftime:%d/%m/%Y (%a)";
The following will work:
// CORRECT
display "{sys%time}" conv="strftime:The time now is %d/%m/%Y (%a).
";
The simple format of {var|conv} is commonly used. However, since {...} always evaluate to a string, if the conversion gives an array, you need to do it with "conv=...". For example:
var a = "(array)x,y";
var b = "(var)a" conv="push:z";
display "{b|list}
";
// Output: 0=>'x',1=>'y',2=>'z'
Please note that variable a has acquired the extra element "z", and b is a copy of a after the conversion. Because the input variable (a) is changed by the conversion "push", we call this conversion "passed by reference".
Some conversions do change the input (passed by reference). They include ++, +=, -, -=, pop and shift. For example:
var a = 2;
var a conv=++;
display "{a}"; // Output: 3
Or
var arr = "(array)x,y";
display "arr was: {arr|list},
";
var item = "(var)arr" conv="pop";
display "but after popping out '{item}'<br>
";
display "arr becomes: {arr|list}
";
// Output:
// arr was: 0=>'x',1=>'y', but after popping out 'y'
// arr becomes: 0=>'x'
Only variables (as specified with the "(var)" type cast) can be passed by reference. If we replace var item = "(var)arr" conv="pop"; with var item = "{arr|pop}";, {item} will still get the popped value ('y'), but variable arr will not change.
With the existing version of BEE, only ++ (or inc), +=, -- (or dec), -=, push, pop, shift and unshift are passed-by-reference conversions.
With user-defined conversions, the input variable can be altered in the conversion function. So it is the programmer of such function who decides whether the conversion is a passed-by-reference or not.
|