Trini

Trini

How can I replace (") by (')

Hello, I have a small problem.
if i have something like:

path = '/home/trini/phoenix-vosk/deps/vosk/priv/vosk-model-small-en-0.42'
model = :vosk.model_new(path)

everything works correctly for me

The problem occurred when I wanted to replace the path with an environment variable and i did something like that:

env = System.get_env("VOSK_MODEL")
model = :vosk.model_new(env)

and it happens that vosk requires that the path be inside single quotes (') but System.get_env("VOSK_MODEL") returns the path in double quotes (")

I’ve tried replacing the double quotes with singles as follows, but to no avail.

cleaned_model_path = String.replace(env, "\"", "'")

clarified that if we do something like

env = System.get_env('VOSK_MODEL')

the function System.get_env gives an error

The question I have is how can I replace (") by (')

Marked As Solved

Trini

Trini

the problem solved I’m left something like this:

   env = System.get_env("VOSK_MODEL")
      c_path = String.to_charlist(env)
      model = :vosk.model_new(c_path)

Also Liked

al2o3cr

al2o3cr

Strings (written with ") and charlists (written with ') are different things. There’s a good introduction in the docs.

String.replace is still going to return a string - the quotes are not part of the value, they are how the value is written literally.

System.get_env expects a string, not a charlist, so it gives you an error if you write System.get_env('VOSK_MODEL')

You need to convert the string value returned from System.get_env into a charlist for the :vosk library - the String.to_charlist/1 function is a good choice for this.

kokolegorille

kokolegorille

You need to use to_charlist when dealing with Erlang libs…

Last Post!

kokolegorille

kokolegorille

This would be the Elixir way to write the same code…

model = "VOSK_MODEL"
|> System.get_env()
|> String.to_charlist()
|> :vosk.model_new()

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement