Sets

Prev Next

Sets

Sets are used to store multiple basic data items, known as elements, into a single variable. Every element may consist of numerals, strings, dates, booleans, voids and sets as well. The embedded sets are also known as nested sets and are useful to create and process multi-dimensional vectors and matrices.

Sets are coded with elements inside braces and separated with commas, e.g. { ape, bee, cat }.

Number of elements At least zero (empty set)
Empty sets Empty sets { } are not the same as blank or void values
Mixed data types A set may contain data of different types, e.g. { 1, Abc, true, date(31.12.2020) }
Nesting Sets support nesting, e.g. { { 1, 2 }, { 3, 4 }, 5 }
Nested empty sets Nested empty sets are allowed. E.g. { {} } is not the same as { } or { { {} } }.
Sequence of elements The sequence of elements is preserved, i.e. {a,b,c,d} and {d,c,b,a} are different.
Comparing them with '=' returns true (ordering does not care), but with '==' returns false (elements must be in same order).
Mulitple identical elements Sets may contain multiple identical elements, e.g. { a, b, a, a, c, c }. You can use the trim() function to eliminate duplicates.
Embedding A paramter set can be embedded into another set, e.g. a[] = {1,2}; b[] = {a[]}; Result: b[] contains {{1,2}}
Indexing Sets One element can be extracted by indexing. 1st element begins wtih 0. Example: a[] = {a,b,c,d}{1}; Here, the 1st element is accessed, a[] gets value 'b'.
Negative indexing Negative indexing supported here. a[] = { a,b,c,d,e }{-2} assigns 'd' to a[].
Slicing Sets Specify multiple index values and ranges to extract subsets.
Arithmetics A broad range of arithmetic operators are available to manipulate sets, for example intersections and unions.

       a[] = { 1, a, 2, {b,c}, true, date(today) };
       echo( a[], " / ", type(a[]) );
       echo( "First element: ", a[]{0}, " and last element: ", a[]{1} );
{1,'a',2,{'b','c'},true,'2025-02-28'} / set
First element: 1 and last element: a
Try it yourself: Open LAN_Features_data_types_Sets.b4p in B4P_Examples.zip. Decompress before use.