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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay