roganjoshua
I am trying to process a text file like a kind of plain text guitar tab with etc. a bit like the Ultimate Guitar Tab site.
Each line will either blanks or chords or lyrics or something like that.
defmodule TextFileProcessor do
def process_file(file_path) do
File.stream!(file_path)
|> Enum.each(&process_line/1)
end
defp process_line(line) when line =~ ~r/^LYRICS:/ do
# Process lines starting with "LYRICS:"
IO.puts("Processing lyrics line: #{line}")
end
defp process_line(line) when line =~ ~r/^CHORDS:/ do
# Process lines starting with "CHORDS:"
IO.puts("Processing chords line: #{line}")
end
defp process_line(line) do
# Default processing for other lines
IO.puts("Processing line: #{line}")
end
end
I can’t use =~ in a guard or other Kernel functions is there a better way to do this?
I hope there is enough in here.
TIA
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
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
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
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Marcus
You can do this with pattern matching in the function args.
roganjoshua
Thank you Marcus, that is very nice, is there any more sophisticated mechanism than <> like regular expressions for example
christhekeele
Not in guards/function heads. You’ll need to use a single function with something like a
condinside.LostKobrakai
There are constraints around guards, which prevent many higher level/more complex apis from being usable:
derpycoder
Here’s my take on it:
or if you don’t care about specifics in the first iteration, you can do:
Both the pipelines, spit out an organized map, which you can then process.
If you don’t want that map, you can directly process it within the pipeline above.
P.S.
Personally, I prefer this way because it makes it composable and allows me to use
dbg()to see if anything in the pipeline is not working as expected.roganjoshua
Thank you all so much for the help
The actual song will likely be in this format plus others like [Chords} to define the kinds of chords to use and other metadata
It matters where along the line the chord def, the text will come from non-tech people so I am expecting a fair amount of handling, things like duplicate lines or mistakes etc.
The idea is to get a nice html render out of it
roganjoshua
If anyone is interested I think I kind of got to where I wanted like this:
Not sure this is optimal but I would be interested in any criticism
dimitarvp
I am pretty late here but I’d advise against regexes unless they are very straightforward. Haven’t looked into yours in details (and length is not always an indicator of a complex regex) but I’d probably reach for
nimble_parsecand give it a go for a day or two.That being said, being productive in that particular library is a skill in and of itself so if you are pressed for time you should probably keep your regex solution but also add edge case unit tests.
roganjoshua
Thanks Dimitar
I think the likely size of the files is going to be pretty small, in the order 100 lines so hopefully regex will do for now.
I will see how I go with it before attempting nimble_parsec.
I am very much at the beginning of my elixir journey.