# Macros and lists

**URL:** <https://bnd.discourse.group/t/macros-and-lists/65>\
**Category:** TIPS\
**Created:** [January 6, 2021, 8:14am UTC](https://bnd.discourse.group/t/macros-and-lists/65 "2021-01-06T08:14:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkriens](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/pkriens/32/3_2.png) [@pkriens](https://bnd.discourse.group/u/pkriens)\
**Post date:** [January 6, 2021, 8:14am UTC](https://bnd.discourse.group/t/macros-and-lists/65/1 "2021-01-06T08:14:54Z")

</div>

The bnd macro language has a _native_ format for lists. A bnd _list_ is a string with comma separated values. For example, `a,b,c` is a list with 3 members. Many macros can provide lists of internal information and many macros can process a list.

For example, `${lsr;.}` is the list of file and directory names in the same directory as the bnd file.

This contains also files that start with a `.` (dot). If you want to _reject_ all files that start with a dot:

```
 ${reject;${lsr;.};\..*}

```

The `${reject;list;regex}` macro takes a list and a _regular expression_ and rejects any matching member. This is unfortunately due to historic reasons not a [GLOB](https://bnd.bndtools.org/chapters/820-instructions.html). There is also a similar `${select;list;regex} that only returns the matching members.

```
 ${select;alfa,beta,charlie,delta, echo;.*ch.*} -> charlie,echo

```

There are many macros that can manipulate lists:

- `[n]sort` – Sort a list alphabetically or numerically
- `uniq` – Remove duplicates
- `[n]min/[n]max` – max or min value alphabetically or numerically.
- `size` – Get the size of a list
- `[s]join` – Join lists
- `sum/average` – Sum or average the values in a list
- `foreach` – Calls a macro for each value and provides the index
- `[last]indexof` – Return the index of an element in a list
- `last/first` – Get last/first element
- `get` – Get an element at a specific place
- `replacelist` – Replace matching elements with a value, providing access to the groups in the original value
- `sublist` – Get a sublist
- `reverse` – Reverse a list
- `retainall` – Retain only elements in both lists

A list is always comma separated. If you need to create a list from a string that uses another separator, you can use the `split` macro.

```
 ${last;${split;\.;com.example.foo.bar}} -> bar

```

Don’t forget that in the bnd editor, you can hover over a selected text with macros to see the expanded form.
