O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a word list

In the Using regular expressions recipe, we used a sigil to define a regular expression. A sigil is an alternative way to define structures that have a textual representation within the language.

This recipe will show you the use of the ~W and ~w sigils to create word lists.

Getting ready

Start a new IEx session by entering iex in the command line.

How to do it…

To define word lists using sigils, perform the following steps:

  1. Define a word list with no interpolation:
    iex(1)> ~W(one two "three" ^ @ \| 12345)
    ["one", "two", "\"three\"", "^", "@", "\\|", "12345"]
    
  2. Define a word list with an interpolation:
    iex(2)> x = 5
    5
    iex(3)> ~w(one two #{x} five#{x} "#{x}")
    ["one", "two", "5", "five5", "\"5\""]
    

How it works…

When using the ~w and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required