Count occurrences of certain string from simple array

I have a simple array that contains strings:

I want to count how many times a certain word occurs in this simple array. Since it can contains hundreds of strings I want to avoid using iterators.

My approach would be to filter the array to contain only the searched word and use the length() function to count the occurrences.

Any advice on how to achieve that? I also can’t use an exclusion process that utilizes the remove() function unfortunately.

Thanks in advance!

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of three modules:

Output

This is just an example. Your final solution may or may not look like this depending on your requirements.

Module Export - quick import into your scenario

You can copy and paste this module export into your scenario. This will import the modules (with fields/settings/filters) shown in my screenshots above.

  1. Move your mouse over the line of code below. Copy the JSON by clicking the copy button on the right of the code, which looks like this:

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the editor.

  3. Click on each imported module and re-save it for validation. There may be some errors prompting you to remap some variables and connections.

JSON module export — paste this directly in your scenario

{"subflows":[{"flow":[{"id":367,"module":"util:ComposeTransformer","version":1,"parameters":{},"mapper":{"value":"test1\ntest2\ntest1\ntest3\ntest2\ntest1"},"metadata":{"designer":{"x":1360,"y":-3121}}},{"id":368,"module":"builtin:BasicFeeder","version":1,"parameters":{},"mapper":{"array":"{{split(367.value; newline)}}"},"metadata":{"designer":{"x":1607,"y":-3121}}},{"id":370,"module":"util:FunctionAggregator2","version":1,"parameters":{"fn":"count","feeder":368},"mapper":{"value":""},"metadata":{"designer":{"x":1848,"y":-3121},"parameters":[{"name":"fn","type":"select","label":"Aggregate function","required":true,"validate":{"enum":["avg","sum","count","max","min"]}}]},"flags":{"groupBy":"{{368.value}}","stopIfEmpty":true}},{"id":371,"module":"builtin:BasicAggregator","version":1,"parameters":{"feeder":370},"mapper":{"__IMTKEY__":"{{370.`__IMTKEY__`}}","result":"{{370.result}}"},"metadata":{"designer":{"x":2091,"y":-3122,"messages":[{"category":"last","severity":"warning","message":"A transformer should not be the last module in the route."}]}},"flags":{"stopIfEmpty":true}}]}],"metadata":{"version":1}}

Note: Did you know you can reduce the size of blueprints and module export code like the above, using the Make Blueprint Scrubber?

Hope this helps! Let me know if there are any further questions or issues.

@samliew

1 Like

Awesome, thanks for helping! :raising_hands:

No problem, glad I could help you with “Count occurrences of certain string from simple array

1. If anyone has a new question in the future, please start a new thread. This makes it easier for others with the same problem to search for the answers to specific questions, and you are more likely to receive help as newer questions are displayed higher on the forum’s “new” page.

2. The Make Community guidelines encourages users to try to mark helpful replies as solutions to help keep the Community organized.

This marks the topic as solved, so that:

  • others can save time when catching up with the latest activity here, and
  • allows others to quickly jump to the solution if they come across the same problem

To do this, simply click the checkbox at the bottom of the post that answers your question:
Screenshot_2023-10-04_161049

3. Don’t forget to like and bookmark this topic so you can get back to it easily in future!

Hope this helps! Let me know if there are any further questions or issues. P.S.: investing some effort into the tutorials in the Make Academy will save you lots of time and frustration using Make!

@samliew