kimc0de
Hi all, I have a .txt file with information like this
11111 0
11112 1
11113 2
11114 3
11115 4
11116 5
11121 6
11122 7
…
66664 zzgl
66665 zzz
66666 zzzz
What I have to do:
- read the file
- return a map like this
%{
“11111” => “0”
“11112” => “1”
“11113” => “2”
…
“66664” => “zzgl”
“66665” => “zzz”
“66666” => “zzzz”
}
Here’s what I have tried
def get_map(file_name) do
{:ok, file} = File.read(file_name)
# create a new map
result_map = %{}
# create a map with key = first word of the line and value = second word of the line
file
|> String.split("\n")
|> Enum.map(fn line -> String.split(line, " ") end)
|> Enum.reduce(result_map, fn [key, value], acc -> Map.put(acc, key, value) end)
end
I got something returned but with errors.
** (FunctionClauseError) no function clause matching in anonymous fn/2 in DiceWare.get_map/1
The following arguments were given to anonymous fn/2 in DiceWare.get_map/1:
# 1
[""]
# 2
%{
"42464" => "manila",
"45512" => "osmose",
"63122" => "unicri",
"31645" => "heimat",
"52552" => "riegel",
"41251" => "leimt",
"55144" => "sierra",
"11223" => "44",
"16131" => "bruch",
"35413" => "kniend",
"41211" => "lehmig",
"45154" => "oase",
"31261" => "habe",
"32216" => "hetzte",
"51641" => "rang",
"64456" => "wenden",
"26435" => "gitter",
"36232" => "krung",
"35163" => "kkkk",
"55213" => "sinus",
"21533" => "donau",
"63116" => "uni",
"53222" => "rosten",
"23533" => "fakten",
"12323" => "abu",
"34462" => "kauer",
"13412" => "anzug",
"23241" => "ernte",
"61324" => "teilt",
"63653" => "vwx",
"44422" => "neogen",
"66312" => "zhd",
"45354" => "onyx",
"11416" => "500",
"64212" => "wamme",
"53351" => "ruhm",
"52316" => "regime",
"63421" => "verl",
"25266" => "fuerst",
"24311" => "fichte",
"24432" => "fj",
"23654" => "faulig",
"12646" => "alias",
"14333" => "baryon",
"21655" => "dronte",
"36513" => "lagern",
"11662" => "$",
"42343" => "magnat",
"53424" => "russig",
"55555" => "spleen",
...
}
(elixirexercise01 0.1.0) lib/diceware.ex:24: anonymous fn/2 in DiceWare.get_map/1
(elixir 1.14.1) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
}
I don’t really understand what I’ve done wrong, why does the map start from the middle of the file?
what do I have to change to make it work?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
adamu
There’s probably a newline at the end of the file that you didn’t account for.
Try
String.trim()beforeString.split("\n"), or doString.split("\n", trim: true).P.S. Posting this during Advent of Code is fortunate timing
mruoss
I’d do something like this. You might have to
Stream.rejectempty lines or so…kimc0de
String.split("\n", trim: true)works like a charm! thank youu!kimc0de
This one works too!