Counting 5-letter words
Tags: miniscript, minimicro, tutorial, challenge
Published: 2022-10-25 20:49:49

---
title: Counting 5-letter words
published: true
description: Solving the 5-letter-word-counting challenge in MiniMicro
tags: miniscript,minimicro,tutorial,challenge
cover_image: https://miniscript.org/w/images/7/74/MiniMicro256.png
# Use a ratio of 100:42 for best results.
#published_at: 2022-10-25 19:53 +0000
---

Over at the [MiniScript community](https://discord.com/channels/646000428441534474/646000428441534478/1034227897180897381) a "daily MiniScript challenge" has been announced.

It consists of [small exercises](https://github.com/JoeStrout/daily-miniscript) to try on [MiniScript](https://miniscript.org/) / Mini Micro.

For a while I have been wanting to write blog posts about Mini Micro - so I take this as an opportunity not only to solve the challenges (the easy part) but to write a blog post about it!

So, let's begin ...

The [first challenge](https://github.com/JoeStrout/daily-miniscript/blob/main/tasks/5-letter-words.md) is called:

## How many 5-letter words are there?

The first challenge is easy: from a given list of words, count how many 5-letter words there are.

The list of words can be found on Mini Micro under:

```
/sys/data/englishWords.txt
```

How do we solve this?

We need to:

1) Open the file
2) Iterate over each line
3) Determine the line's length
4) Increase a counter if the length is 5
5) Print the result

A file is opened with [`file.open`](https://miniscript.org/wiki/File.open). It returns a [`FileHandle`](https://miniscript.org/wiki/FileHandle) object.

In turn, we can ask the `FileHandle` to read a line (with `readLine`) and also if we are at the end of the file (with [`atEnd`](https://miniscript.org/wiki/FileHandle.atEnd)).

Let's try that:

```
] wordsFile = file.open("/sys/data/englishWords.txt")
] wordsFile.atEnd
0
```

Let's read a some lines:

```
] wordsFile.readLine
a
] wordsFile.readLine
aah
] wordsFile.readLine
aardvark
] wordsFile.readLine
aardvarks
] wordsFile.readLine
abaci
] wordsFile.readLine
aback
```

We see it works. With this we have the necessary elements iterate over all lines. 

Over at the editor we could type something like this:

```
wordsFile = file.open("/sys/data/englishWords.txt")
while not wordsFile.atEnd
	word = wordsFile.readLine
	print word
end while
```

Note how we are iterating in a loop while the file is **not** at the end. Don't forget the `not` operator.

This will print all words contained in the file ... but they are a lot! After some seconds I interrupted the program with CTRL-C

We could instead _count_ the amount of lines. Let's do that instead:


```
count = 0

wordsFile = file.open("/sys/data/englishWords.txt")

while not wordsFile.atEnd
	word = wordsFile.readLine
	count = count + 1
end while

print "The file contains " + count + " words"
```

Now, this was much faster. My result was:

```
The file contains 64664 words
```

Yours? Should be the same. Unless the list got updated.

This program is now very close to what we wanted to achieve: count **only** the words that are 5 letters long.

In order to measure a string's length we use the intrinsic function [`len`](https://miniscript.org/wiki/Len), which works on lists, maps and strings.

The program then needs only a small change. Instead of just always counting each line:

```
count = count + 1
```

... we will do so conditionally. Change the line above for:

```
if len(word) == 5 then count = count + 1
```

This can be written in one line as above or alternatively as:

```
if len(word) == 5 then 
    count = count + 1
end if
```

It would be also appropriate to change the result-message. Something like this would do:

```
print "There are " + count + " 5-letter words"
```

Try the program now. What happens when you run it?

I get:

![program-result showing amount of 5-letter words](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/psqsnssqnj6bzj7visb4.png)

Did you also get that?

This is the end version of my program:

![end-version of the program](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/733yw8jtons7qx1yzsa4.png)

Hope this mini-challenge was fun and you learnt some MiniScript / MiniMicro.

See you in the next one!