wency
wency

Reputation: 13

How to include a variable in io:get_line (Erlang)

Is there a way to put a variable in a io:get_line() code? I tried

io:get_line("~s: ",[Variable]).

but it doesn't work. So my question is there another implementation for this?

Upvotes: 1

Views: 266

Answers (2)

Dogbert
Dogbert

Reputation: 222398

You can pass the format string and arguments to io_lib:format/2 first, and then send that to io:get_line/1:

1> Variable = "Name".
"Name"
2> io:get_line(io_lib:format("~s: ", [Variable])).
Name: Dogbert
"Dogbert\n"

Upvotes: 2

P_A
P_A

Reputation: 1818

You can use fread/3 function:

Reads characters from the standard input (IoDevice), prompting it with Prompt. Interprets the characters in accordance with Format. Format contains control sequences that directs the interpretation of the input.

Upvotes: 0

Related Questions