# Converting List of String to List of Int

I am trying to learn some elixir with this years advent of code (no spoilers please).

Ex: I have in a file

four9one
bbzhsmnmtf8kftwosevenxfkssgrcjthree
6pkkcddsixsixjgnjvdtjtwo
4four45seven7nine7two
rcssix4

When I run my function I see the expected numbers individually but not as a List.

``````iex(69)> AdventOfCode2023.day_one
99
88
66
47
44
cXB/, # What is this? In my head I think it should be [99, 88, 66, 47, 44]
``````

I am just confused at what is happening in my function. I don’t just want a solution to the problem itself.

``````  def day_one do
numbers =
|> String.split("\r\n", trim: true)
|> Enum.take(5)
|> Enum.map(fn raw ->
word = String.replace(raw, ~r/\D/, "")

if String.length(word) === 1 do
IO.puts(String.to_integer(word <> word))
String.to_integer(word <> word)
else
IO.puts(String.to_integer(String.first(word) <> String.last(word)))
String.to_integer(String.first(word) <> String.last(word))
end
end)

IO.puts(numbers)
end
``````

Charlist. It is just different representation, just like `0xFF == 255`.

1 Like

I guess my next question would be why is it a charlist and not [99, 88, 66, 47, 44]

You have it described in the link I have posted. It is exactly the same thing, just different representation.

2 Likes

Every newcomer is confused by this notation…

``````iex> 'hello world' |> IO.inspect(charlists: :as_list)
[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
~c"hello world"
``````

It’s coming from Erlang… where charlist is used as “string”
Every time there is a list that could potentially be something readable, it will print it like this

4 Likes

I see, I miss understood the first time I read it.

FWIW @Marzdor you can override this behaviour in IEx by adding this to your `\$HOME/.iex.exs` file:

``````IEx.configure(inspect: [charlists: :as_lists])
``````

You can also pass `charlists: :as_lists` to any `IO.inspect/2` call, or set this project-wide by adding this code somewhere:

``````inspect_opts_default_inspect_fun = Inspect.Opts.default_inspect_fun()
Inspect.Opts.default_inspect_fun(fn value, opts ->
opts = Map.put(opts, :charlists, :as_lists)
inspect_opts_default_inspect_fun.(value, opts)
end)
``````
2 Likes

Thank you for the tip.