ambareesha7
i stuck with this problem,
i could read the text from text-file and index it but i could not update
I’m missing something
defmodule ReadText do
def read_text_file(file_name) do
case File.read(file_name) do
{:ok, text} ->
IO.puts("slice 1: #{String.slice(text, 3..8)}")
slice1 = IO.gets("slice 1 to replace: ")
String.replace(text, String.slice(text, 3..8), slice1, global: false)
IO.puts("slice 2: #{String.slice(text, 72..80)}")
slice2 = IO.gets("slice 2 to replace: ")
String.replace(text, String.slice(text, 72..80), slice2, global: false)
IO.puts("slice 3: #{String.slice(text, 86..91)}")
slice3 = IO.gets("slice 3 to replace: ")
String.replace(text, String.slice(text, 86..91), slice3, global: false)
IO.puts("slice 4: #{String.slice(text, 101..110)}")
slice4 = IO.gets("slice 4 to replace: ")
String.replace(text, String.slice(text, 101..110), slice4, global: false)
{:error, error} ->
IO.puts(error)
end
end
def get_replaceable_indexs(file_name) do
case File.read(file_name) do
{:ok, text} ->
text
|> String.split("")
|> Enum.with_index(fn v, i -> [i, v] end)
{:error, error} ->
IO.puts(error)
end
end
def open(file_path) do
File.open(file_path, [:read, :write], fn text ->
IO.read(text, :all)
end)
end
end
i tried in livebook to get dynamic index for square brockets and use this index as rang in String.slice(text, 3..8) but i’m missing the logic
i highly appreciate any help
thank you
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 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Values in Elixir are immutable (they cannot be changed once created) - functions like
String.replacereturn a new binary.You can rebind variables, however. For instance,
After this line, the name
textwill refer to the result ofString.replaceinstead of the original input.General note: hardcoding numerical offsets that are passed to
String.sliceis very likely not what the problem is really looking for. Take a look at theRegexmodule for a better way to find sequences like “left square bracket followed by letters followed by right square bracket” and manipulate them.dimitarvp
Using string slices is absolutely not what you want here. I’d tell you that you failed the interview if you showed that to me.
Look for ways to search
[anything]in the source text and replace that.Regexis a good start and might even be good enough as a final solution.ambareesha7
Yeah I know hard coding numbers are not the proper solution here and I have theoretical solution but struggling to implement that,
ambareesha7
I’m a newbie getting into programming and trying my share of struggles, I’ll learn as I practice,
I’ll try regex functions
ericgray
If I understand the problem correctly you need to replace variable placeholders like
[name]with passed in arguments. You can use a Regex to solve this but if the structure ofsource.txtis exactly as it appears you can also use Elixirbinary pattern matching. You can recurse over a binary file and match patterns like[name][company][time][salesguy].To keep things simple you can pass in a map as an argument
Now with
binary pattern matchingyou can use this map to replace the placeholder variables.Now in
iexyou canambareesha7
Thank you @ericgray it works and I’m trying on regex implementation,
still reading different regex and string related doc’s, articles
ericgray
Great that’s a good way to learn. Try different things to see what works best for you. Regex patterns are good but they can be cryptic and hard to read. I think in this case where you know the shape of the data before hand
binary pattern matchingis easier in my opinion. Try a Regex and let us know what you come up with.Aetherus
Suppose you have a map that stores the attributes you want to stuff into the template, like
you can try
or
al2o3cr
Given
attrs:and an input string in
source, a single call toRegex.replacecan do this:This will silently replace unrecognized keys with empty strings; use something like
Map.fetchif that isn’t desired.The regex here looks worse than it is, because square brackets are metacharacters in regex:
\[matches a literal open bracket([^\]]+)captures one or more characters that aren’t a]\]matches a literal close bracketAetherus
That’s faster than my solution, I guess, since it goes through the template string only once.
If the keys contain only word characters (i.e.
atoz,AtoZ, numbers, and_), the regex can be simplified as