[erlang-questions] how do I convert a character to an integer (dict broke the integer I put into it)

Robert Virding <>
Sun Dec 16 19:30:05 CET 2007


On 16/12/2007, Dominic Chambers <> wrote:
>
> Yes, the following gives me a character instead of a number (I put a
> number in but I got a character out -- which may, as you say, be
> identical anyway):
>
>   dict:fetch("Key", dict:append("Key", 33, dict:new())).
>
> but then the problem I get is that if I try to add 1 to that number I
> get a badarith error:
>
>   dict:fetch("Key", dict:append("Key", 33, dict:new())) + 1.
>
> I have just made misunderstood what is happening here?


As matthias said dict:append and dict:store are different. Dict:store stores
the value under the key while dict:append appends the new value to a list of
the existing values. If there are no values then the new value is
automatically put in a list. So you get:

1> dict:fetch("Key", dict:store("Key", 33, dict:new())).
33
2> dict:fetch("Key", dict:append("Key", 33, dict:new())).
"!"
3> dict:fetch("Key", dict:append("Key", 35, dict:append("Key", 33,
dict:new()))).
"!#"
4>

Dict:fetch (and dict:find) always return the whole value which might be a
list of values or one value.

Robert

P.S. Dict:append will also make a list of value which is already there and
not a list. This is probably a mistake, but we were trying to be nice.
Better to be consistent than nice.

P.P.S. Orddict has exactly the same interface but the dictionary is easier
to understand when printed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20071216/54ea3024/attachment.html>


More information about the erlang-questions mailing list