anubis
anubis

Reputation: 1505

How obtain in Erlang with fread a string with no " "

I have a fread in my program:

{ok, [S]} = io:fread("entry: \n", "~s")

But I get {ok, "string"}

I want to get just the string and not the quotation marks.

So I can use it in:

digraph:add_vertex(G, S)

And receive a vertex string and not "string"

How can I do that?

Upvotes: 2

Views: 724

Answers (1)

Michael
Michael

Reputation: 3729

It's an illusion. The quotation marks aren't really there. The shell, or other display mechanisms may show them when rendered with certain contexts in order to represent the data you are displaying, but the quotes in this case are really just meta data:

Here's your case:

1> {ok, [S]} = io:fread("entry: \n", "~s").
entry: 
foo
{ok,["foo"]}

If you display S strictly though, you will see that it is a list with only 3 characters:

2> io:format("~w~n", [S]).                 
[102,111,111]
ok

If you ask io:format/2 to display the data generically, using it's best representation of the interpretation of the data though, it thinks 'ahha, this is a string, I shall display it as a string':

3> io:format("~p~n", [S]).
"foo"
ok
4> 

Strings are obviously just lists, so in this that case a decision has to be made to display as a string, or as a list, and the decision to display as a string is made because the list bytes all represent printable characters. Adding a non printable character will change the behaviour therefore, like this:

5> io:format("~p~n", [[2|S]]).  
[2,102,111,111]
ok
6> 

Upvotes: 9

Related Questions