Introduction

The bracket operator is the use of '[' and ']' like accessing arrays in C.

In YCP, this operator is used to ease handling with (possibly nested) lists and maps.
The bracket operator can be applied to any list or map variable and should be used in favour of (deeply) nested lookup() and select() cascades.

Access variant


The access variant of the bracket operator is used for accessing elements of a list or a map. It effectively replaces select for lists and lookup for maps.

Accessing lists

General syntax:
for simple lists:
<list-var>[<index>]:<default-value>
for nested lists:
<list-var>[<index>,<index> <, ...>]:<default-value>


index must be an integer and counts from 0 up to the number of elements-1.
It will return the default-value if you try to access an out-of-bounds element.

Note that there must be no space between the closing bracket and the colon.

Examples:

{
  list l = [1, 2, 3];

  integer three = l[2]:0;		// == 3

  integer zero = l[42]:0;		// default value

  list ll = [[1,2], [3,4], [5,6]];

  return (ll[1,0]:0 == three);	// returns true
}

Accessing maps

General syntax:
for simple maps:
<map-var>[<key>]:<default-value>
for nested lists:
<map-var>[<key>,<key> <, ...>]:<default-value>


key must have an allowed type for maps, integer, string, or symbol.
It will return default-value if you try to access an non existing key.

Note that there must be no space between the closing bracket and the colon.

Examples:

{
  map m = $["a":1, "b":2, "c":3];

  integer three = m["c"]:0;		// == 3

  integer zero = m["notthere"]:0;	// default value

  map mm = $["a":$[1:2], "b":$[3:4], "c":$[5:6]];

  return (mm["b",0]:0 == three);	// returns true
}

Mixed map/list access

Since the bracket operator applies to list and maps, you can use it to access nested lists and maps. But be careful not to mix up the index/key types.

Examples:

{
  map map_of_lists = $["a":[1, 2, 3], "b":[4,5,6], "c":[7,8,9]];
  integer three = map_of_lists["a",2]:0;		// == 3
  list list_of_maps = [$[1:2], $[3:4], $[5:6]];
  return (list_of_maps[1,0]:0 == three);		// returns true
}

Assign variant

The bracket operator can also be used on the left side of an assignment (lvalue).
This changes the list or map element in place (!!) and must be used with care. See the alias trap for details.

If the map or list element does not exist, it will be created. The bracket operator can therefore replace add and change.
Creating a new list element will extend the size of the list. Holes will be filled with nil. See the examples below.

If used as an lvalue, the default value is not allowed.

Examples:

{
  list l = [1,2,3];

  // change the second element
  l[1] = 25;			// l = [1,25,3] now !

  // change the "c" element
  map m = $["a":1, "b":2, "c":3];
  m["c"] = 42;			// m = $["a":1, "b":2, "c":42] now

  // extend the list to 7 elements (0-6)
  l[6] = 6;			// l = [1,25,3,nil,nil,nil,6] now !

  // add a new element to m

  m["zz"] = 13;

  return (mm);			// $["a":1, "b":2, "c":42, "zz":13]
}

Again: Be careful with the assignment variant of the bracket operator and don't fall into the alias trap.


Klaus Kämpf
Last modified: Wed Nov 28 15:21:42 MEST 2001