Methods
This is a comprehensive list of the available methods to scalars, hashes and lists.
There are a number of ways to accomplish certain actions with your data, whether they're scalar (also known as a string), a list (also known as an array), or a hash.
Scalars are single unit variables such as a number or a string. A string is a single unit as it is treated as one thing between " ". This is a comprehensive list of their available methods.
.length
.length will return the length of the string in blocks used, this means it includes whitespace as well as characters.
This can be useful for inputs that require a certain number of digits, such as a mobile phone entry:
[* mobile_phone = "07700 456789" *]
[* IF mobile_phone.length < 11 *]
[* "Number too short!" *]
[* END *]
Output: Number too short!
.lower
.lower will put all characters to lower case; useful if you wish to record customer input data in a way that is consistent, to aid searching through it in future.
[* user_email = "AbcdeFg@h.mail.com" *]
[* user_email.lower *]
Output: abcdefg@h.mail.com
.upper
.upper changes all characters to UPPER case. This may be useful for differentiating between first name and surname.
[* surname = "de la Rosa" *]
[* surname.upper *]
Output: DE LA ROSA
.match
This will perform a regular expression match on the string using the pattern as an argument. You can use this if you want to search for a first or a surname and the data input includes them both.
[* full name = "Charmain de la Rosa" *]
[* matches = name.match('(\w+) (\w+)') *]
[* matches.0 *]
Output: Charmain
If you want to search for the surname in this example the code becomes more complex, and is explained in detail in the Complex Name Matching example.
Complex Name Matching
We have used the example name of Charmain de la Rosa in the documentation, regarding .match. It is possible that a number of your customers have surname prefixes, ideally you would request a first name and surname separately—should you know this may occur. However, if you only have a 'Full Name' input option you can still manage this.
[* name = 'Charmain de la Rosa' *]
[* matches = name.match('\b(\w+)', 1)*]
[* first_name = matches.shift *]
[* surname = matches.join(' ') *]
[* surname *]
Output: de la Rosa
By placing the \b at the start of the expression and providing a true value (1), we can now globally search the 'name' string for all groups of characters.
Using .shift removes the first item from a list and returns it, if you administer it to a variable (in this case 'first_name' it will be stored in your data rather than returned). Using .join on 'matches', by declaring the join character as a space (' '), will join all further list items to create a string.
Making this a new variable (surname), you can then output just the surname.
This will also work for names with an apostrophe, which you must remember to escape; though the output will omit the apostrophe when returned (e.g. O'Connor becomes O Connor).
.replace
With .replace you can change the specified item to something else, giving you consistency in the structure of a string without changing its properties.
The example uses personal interests that haven't needed to be comma spaced, leading to an inconsistency in listing which would cause issues if you wanted to .split, discussed below.
[* my_string = "sport, movies books, sitcoms & coffee" *]
[* my_string.replace('\W+', ',')*]
Output: sport,movies,books,sitcoms,coffee
Note: Make sure to use a capital W in your argument. \w finds word values, whereas \W is a negation of this and finds the non-word values in your string.
.remove
If you need to log data, such as a phone number, without spacing you can use the .remove method.
[* phone_number = "07700 456 789" *]
[* phone_number.remove('\W+') *]
Output: 07700456789
.search
This is similar to .match but works as a boolean and will output your chosen true or false values.
[* my_string = "sport,movies,books,sitcoms,coffee" *]
[* my_string.search('books') ? 'books' : 'no books' *]
Output: books
.substr
.substr allows you to put in the parameters of 'offset' and 'length', or where to begin an output and how much.
If length is not specified it will output all blocks from the offset to the end of the string.
[* domain_url = "http://www.website.com" *]
[* domain_url.substr(11) *]
Output: website.com
Replacement
You can also use the offset and length alongside a replacement value; for example you can change http:// to https:// to change it to a secure URL.
[* domain_url = "http://www.example.com" *]
[* domain_url.substr(0, 4 ,'https') *]
Output: https://www.example.com
.split
This lets you convert a string to an array by selecting a separation value, which will in turn allow you to do a FOREACH loop through the data.
For example:
[* my_haiku = "This is a haiku|on unseparated lines|but not for too long" *]
[* haiku_array = my_haiku.split('\|' ) *]
[* FOREACH line IN haiku_array *]
[* line *]
[* END *]
This code splits my_haiku up at each '|' and haiku_array becomes an array by being set as the .split string. Now we have an array the FOREACH goes through each 'line' (which you could call anything; item, thing etc.) and outputs that 'line'. Make sure you add a '\' before '|' for the feature to work as expected.
Which would look like this:
This is a haiku
on unseparated lines
but not for too long
.chunk
If the string provided by a customer needs to be split into even parts you can use .chunk.; a sort code, for example:
[* sortcode = "001122" *]
[* sortcode.chunk(2).join *]
Output: 00 11 22
Note: You also need to use .join to return the output; otherwise, it will output something like this: ARRAY(0x543064051)
You can also 'chunk' from right to left, for example if you need to split large numbers, by using a negative in the 'chunk' value:
[* current_balance = "99999999" *]
[* current_balance.chunk(-3).join(',') *]
Output: 99,999,999
Lists, also known as arrays, are multiple pieces of data within a variable. This is a comprehensive list of the methods available to them.
.first/.last
To return the first or last value of your array:
[* my_array = ['eggs', 'flour', 'milk', 'sugar'] *]
[* my_array.first *]
[* my_array.last *]
Output: eggs sugar
You can also add a numeric argument to return (x) number of items in your list but you must remember to .join this for a list to be outputted.
[* my_array.first(3).join(', ') *]
Output: eggs, flour, milk
.size/.max
.size returns the number of items of a list:
[* my_array.size *]
Output: 4
.max returns the maximum index number or the size minus 1:
[* my_array.max *]
Output: 3
.defined
This will return a true or false output (of your choosing), if there is an item assigned to the 'space' you give it.
Note: List blocks begin at 0.
[* my_array.defined(2) ? 'y' : 'n' *]
Output: y
Whereas a .defined(4) on my_array would return 'n'.
.reverse
Will reverse the order of your list:
[* FOREACH item IN my_array.reverse *]
[* item *]
[* END *]
Output: sugar milk flour eggs
.join
Will join the items into a single string, spacing them with your given value:
[* my_array.join(', ') *]
Hashes are made up of key=value pairs and are contained within braces {}. Below is a comprehensive list of their available methods.
Calling a Hash Value
Calling hash values is easy, you just need to call the key.
[* my_hash = {
cleveland = 'browns'
miami = 'dolphins'
new_york = 'jets'
seattle = 'seahawks'
}
*]
[*
my_hash.cleveland
*]
Output: browns
.keys
This will return the keys within a hash, though not in any particular order (to do this add .sort after the keys method).
[* my_hash = {
cleveland = 'browns'
miami = 'dolphins'
new_york = 'jets'
seattle = 'seahawks'
}
*]
[* FOREACH key IN my_hash.keys *]
* [* key *]
[* END *]
Output: * new_york * seattle * miami * cleveland
You can also output the values using the .$key, which will search for the key within the key values:
[* FOREACH key IN my_hash.keys *]
* [* key *] (nickname: [* my_hash.$key *])
[* END *]
This would output a list that begins: * new_york (nickname: jets)
.values
Similar to .keys, although this will output the values in the hash.
[* FOREACH value IN my_hash.values *]
* [* value *]
[* END *]
.pairs
Like using .$keys this will return a list of keys and values.
[* FOREACH pair IN my_hash.pairs *]
* [* pair.key *] known as [* pair.value *]
[* END *]
.items
You can return a list of both keys and values with this, though without a .join method you will only return an array.
[* my_hash.items.join(',') *]
Output: new_york,jets,seattle,seahawks,miami,dolphins,cleveland,browns
.sort
You can alphabetise (or numerically order using .nsort) according to the values in the hash.
[* FOREACH nickname IN my_hash.sort *]
[* my_hash.$nickname *]
[* END *]
Output: * browns
etc.
.import
Using .import allows you to place one hash in another, useful if two hashes contain the same key and value headings but with different information.
[*
my_hash = {
cleveland = 'browns',
miami = 'dolphins',
new_york = 'jets',
seattle = 'seahawks',
}
*]
[*
new_hash = {
cincinatti = 'bengals',
san_francisco = '49ers',
denver = 'broncos',
}
*]
[* my_hash.import(new_hash) *]
[* my_hash.denver *]
Output: broncos
The example ends with a search in the first hash, for a key which was in the second.
You can also use import() to then list the hash data:
[* nfl_teams = { place = "Cleveland", nickname = "Browns" } *]
[* import(nfl_teams) *]
[* place *] : [* nickname *]
Output: Cleveland : Browns
.defined/.exists
You can search through a hash's keys and be provided with a true or false value, if the argument passes the .defined or .exists argument:
[* my_hash.defined('cleveland') ? 'y' : 'n' *]
Output: y
.delete
Delete one or more items from a hash.
[* my_hash.delete('miami', 'new_york') *]
.size
Returns the number of key/value pairs within the hash.
[* my_hash.size *]