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

Matthias Lang matthias@REDACTED
Sun Dec 16 11:46:29 CET 2007


Dominic Chambers writes:

 > I add some integers as values into a dict, but when I go to retrieve
 > them they are now strings. After an hour of googling the only thing I
 > can find online is that $c does what I want for character literals,
 > but this doesn't seem to work for character variables. I need this
 > because adding to the number I get out of the dict gives me a badarith
 > error.

You didn't provide any examples of what you mean. Here's an example
which seems to contradict what you're saying:

   1> A = dict:new(), B = dict:store(this_is_my_key, 65, A).                       {dict,1,
         16,
         16,
         8,
         80,
         48,
         {[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
         {{[],[],[],[],[],[],[],[],[],[[this_is_my_key|65]],[],[],[],[],[],[]}}}
   2> C = dict:fetch(this_is_my_key, B).
   65
   3> C + 9.
   74
   
A stab in the dark: one explanation for "why would Dominic write the
above" is that you have not understood the difference between
dict:store/3 and dict:append/3? Remember, in Erlang a string is
represented as a list of integers.

Matt



More information about the erlang-questions mailing list