Our second project is a simple command line script that counts the words in a file, much like the Unix command
We'll see some differences between writing Elixir modules and one-off scripts and we'll see how to read a file. The most difficult part of the lesson is deciding how to split the file into words. We'll get a taste of the both the joy and the terror of regular expressions in the process.
Source code for this episode
IO.gets to ask user the name of a file to open
String.trim to get rid of trailing enter they press
Split the string into words with
String.split and a regular expression
Filter out non words with
Count the words with
The middle portion of the video also includes a detour showing some basics of regex and working with them in Elixir.
mix utility is not used at all in this lesson. Neither is
escript. Both are covered in a future lesson but there's some value in working with the very basics first, so that's what we did.
See if you can extend the script build in this lesson so that the user can optionally count the number of lines or characters a file instead of the number of words.
Hint: The challenge in this lesson will be much easier if you look at the docs for Enum and see what Enum.each and Enum.at do.