Getting lists from ASCII input

Juan José Sánchez Penas jjsanchez@REDACTED
Tue May 16 01:40:29 CEST 2006


Hi Deryk,

On Mon, May 15, 2006 at 02:15:25PM -0700, Deryk Barker wrote:
> I cannot quite figure out what combination of list_to_atom and
> atom_to_list I need to do this.
> 
> e.g. if the user enter:
> 
> 	remove [a,b,c]
> 
> I'd like to be able to get the remove as an atom and the [a,b,c] as a
> list of three atoms. Currently, I can get the list

I guess for the case of the list you need to scan (obtain Tokens) and then
parse (convert to abstract form and then to the corresponding Erlang term).
For the command part you can use directly list_to_atom (converts strings
into Erlang atoms).

One way of doing it would be:

parse_user_input(UserInput) ->
  [ComString, ArgString] = string:tokens(UserInput, " "),
  ComTerm = list_to_atom(ComString),
  {ok, ArgTokens, _} = erl_scan:string(ArgString++"."),
  {ok, ArgTerm} = erl_parse:parse_term(ArgTokens),
  {ok, ComTerm, ArgTerm}.

The code assumes the input string only has spaces between the command and
the list.

1> test:parse_user_input("remove [1,2,3]").
{ok,remove,[1,2,3]}

Hope it helps :)

Best regards,

--
Juan José Sánchez Penas



More information about the erlang-questions mailing list