Removing table columns based on a filter using their name

Sometimes you may want to delete some column(s) in a data table based on a filter criteria. Perhaps, you want to delete all columns starting with “OLD_”. We can unleash the power of Groovy here. Let’s assume you have some data like this:

We’ve run the task using Run to Line (Ctrl+R) and then opened the Variables view from the Views toolbar. In the Variables view you can see all the variables currently in scope. $lastReturnValue just returns the value of the last step as the name suggest.

The Save Table Reference step saves the data from to a variable called people

Now to explain the formula which does the magic trick:

=people.getHeader().findAll{it.startsWith("OLD_")}.join(",")

What we’re doing here is:

  1. Getting the table header using getHeader() method of ORQA table. It returns a list
  2. On that list we apply findAll method using a closure as the criteria - this is somewhat difficult to grasp so for further reference here is a post that explains it somewhat Groovy filter criteria on findAll on a list - Stack Overflow. The criteria we’re applying returns all the entries in the list starting with “OLD_”
  3. Now we can join the list to get a comma separated list of the columns to remove.

Having stepped over the remove step, we can see the final result with the columns removed.