DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
hiclab
hiclab

Posted on • Updated 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)

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈