DEV Community

hiclab
hiclab

Posted on • Edited on

Count occurrences of an element in a list in RCPTT

For those who don’t know, RCPTT (RCP Testing Tool) originally known as Q7 IDE, is an open source project with a focus to test Eclipse plugins and RCP applications. For more details, visit the RCPTT website.

Let’s assume we have a list of strings and we want to count the frequency of a given string. We will use the command each to iterate over the list and check whether it contains the string.

Let’s create a procedure in ECL, the scripting language used by RCPTT.

proc countOccurrences [val items -input] [val targetItem] { $items | each [val it] { if [$it | eq $targetItem] { bool true } } | length } 

The trick here is to return some value in the if section every time the string is found. In our example, we return a boolean but actually we can return any other value. This is important in order to count the number of returned values at the end.

Run some tests.

global [val items [list a b c a d e a a]] $items | countOccurrences a | log $items | countOccurrences p | log 

The output.

!MESSAGE 4 !MESSAGE 0 

Top comments (0)