Macros and lists

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. 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.