Getting lists from ASCII input

Gaspar Chilingarov nm@REDACTED
Tue May 16 10:34:24 CEST 2006


Matthew McDonnell wrote:
> On Mon, 15 May 2006, Deryk Barker wrote:
> 
>> I'm trying to write a program (a client - my students will write the
>> server) which needs to allow the user to enter a list in repsonse to a
>> prompt.
>>
>> 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
> 
> io:scan_erl_exprs does just about what you want:
> 
> 1> io:scan_erl_exprs('enter text>').
> enter text>remove [a,b,c].
> {ok,[{atom,1,remove},
>      {'[',1},
>      {atom,1,a},
>      {',',1},
>      {atom,1,b},
>      {',',1},
>      {atom,1,c},
>      {']',1},
>      {dot,1}],
>     2}
> 
> Something like the following maybe? (untested, probably typos):
> 
> parse() ->
>   {ok, [ActionAtom, {'[',_}, AtomList], _Num} = io:scan_erl_exprs(),
>   List = accumulate(AtomList,[])
>   {ActionAtom, List}.
> 
> accumulate([{atom, _, At} | Lst], Acc) ->
>   accumulate(Lst, [At | Acc]);
> accumulate([{']',_} | ], Acc) ->
>   lists:reverse(Acc).

in you input correct erlang term, you can parse them easily ;)

1> {ok, E, _} = io:scan_erl_exprs('enter text>').
enter text>[a,b,c].
{ok,[{'[',1},{atom,1,a},{',',1},{atom,1,b},{',',1},{atom,1,c},{']',1},{dot,1}],
    2}
2>  erl_parse:parse_term(E).
{ok,[a,b,c]}
3>





-- 
Gaspar Chilingarov

System Administrator,
Network security consulting

t +37491 419763 (mob)
i 63174784
e nm@REDACTED



More information about the erlang-questions mailing list