命名变量时添加参数
我想为每个身份号码创建一个字典。
传入的参数可以是例如 {Id, Key, Value}
我想通过将 Id
附加到前缀(例如 Dict
然后我想使用 dict:store(Key, [Value], oldDict)
将 Key 写入字典,
此时的 Value
是一个队列,所以我将首先读取队列并添加到队列中,然后写回到字典中。
我的问题是如何将 Id 附加到前缀上?
I would like to create a dictionary for each Identification number.
The argument passed in could be e.g. {Id, Key, Value}
I want to create a new dictionary by appending the Id
to a prefix e.g. Dict
Then I want to write the Key to the dict using dict:store(Key, [Value], oldDict)
The Value
at this point is a queue, so I will first read the queue and add to it then write back to the dictionary.
My question is how does the Id get appended to the prefix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想在运行时构造一个变量名
这是不可能的,抱歉。
在 Erlang 中,您无法在运行时为事物附加名称†。您唯一的选择是将所有字典存储在另一个字典中并传递它。
为了将静态标识符 (D) 和数字粘合在一起,Erlang 为您提供了多种可能性。最简单的方法是构造一个元组
{d, ID}
并将其用作键。您还可以使用下面描述的任何字符串连接方法。
现在你已经澄清了你的问题,我原来的大部分解释都失去了背景。无论如何,我要把他们留在这里。
你想要连接一个字符串和一个整数
这很简单,只需使用:
你想要在字典中设置一个值,使用上面的作为键
Erlang 是一种函数式语言,所以破坏性地修改字典是不可能的。
假设您有一个 字典 存储在变量
Dict
中并且SomeID
设置为 3。您可以使用以下方法获取
Dict
的副本,其中“D3”设置为Value
:您正在寻找类似 printf 的机制
Erlang 函数
io_lib:format (FmtString, Args)
可以用于此目的。与printf
不同,格式化指令以波形符字符开头。您可以在手册中查找正确的指令,但是对于将一个整数添加到字符串
"D"
中,调用将如下所示:问题:
io_lib:format/2
返回一个深层列表人物。如果您想要一个平面列表,请在结果上使用lists:flatten/1
。你可以在 Erlang shell 中尝试一下:† actually, you can do that using the process dictionary. Don't! You'll lose the ability to properly
test and debug your program (apart from going to hell).
You want to construct a variable name at runtime
That's impossible, sorry.
In Erlang, you cannot attach names to things at runtime†. Your only option would be store all dictionaries in another dictionary and passing that around.
For glueing together a static identifier (D) and a number, Erlang gives you quite a number of possibilities. The easiest way would be to construct a tuple
{d, ID}
and using that as the key.You can also use any of the string concatentation methods described below.
Now that you've clarified your question, most of my original interpretations have lost context. I'm leaving them here, anyway.
You want to concatenate a string and an integer
This is easy, just use:
You want to set a value in a dictionary, using the above as the key
Erlang is a functional language, so destructively modifiying a dictionary is not possible.
Supposing you have a dictionary stored in the variable
Dict
andSomeID
is set to three.You can obtain a copy of
Dict
in which "D3" is set toValue
using:You are looking for a printf-like mechanism
The Erlang function
io_lib:format(FmtString, Args)
can be used for that. Unlikeprintf
, formatting directives start with a tilde character.You can look up the right directive in the manual, but for adding an integer to the string
"D"
, the call would look like this:Gotcha:
io_lib:format/2
returns a deep list of characters. If you want a flat list, uselists:flatten/1
on the result. You can try this in the Erlang shell:† actually, you can do that using the process dictionary. Don't! You'll lose the ability to properly
test and debug your program (apart from going to hell).
Erlang 中的
dict
结构不允许您为其命名。而且dict
中的名字可能毫无意义。dict
只是一个键/值字典,其表示形式未定义。如果您想要引用多个字典,那么将它们存储在记录或元组中以“命名”它们可能是个好主意。您不应该从函数参数中创建变量名。
假设您需要将字典名称设置为 d_mycustomname,您可以这样写:
作为其他受访者,我不确定这是否正是您所要求的。请重新表述您的问题以获得更好的答案。我现在的回答只是酒精驱动下的猜测。
更新后:
关于向字典添加值的方式,您想要的可能是
更新
而不是存储
操作。这会将您的值附加到“队列”中。如果尚不存在,它将创建一个。
关于前缀名称,您可以将字典存储在 proplist 中:
我没有测试代码,这只是让您了解我的建议。
The
dict
structure in Erlang doesn't allow you to give it a name. And it's probably pointless to have a name in adict
. Adict
is a mere key/value dictionary, whose representation is not defined.If you have multiple dictionaries that you want to refer to, it's probably a good idea to store them in a record or in a tuple, to "name" them. You shouldn't create variable names out of function parameters.
Supposing you need to have the dictionary names as d_mycustomname, you could write something like:
As the other respondents, I'm not sure this is exactly what you've asked for. Please re-formulate your question to get better answers. My answer at this point is just guess-work driven by alcohol.
AFTER YOUR UPDATE:
Regarding the way you add values to the dictionary, what you want is probably an
update
and not astore
operation.That will append your value to the "queue". If not present yet, it will create one.
Regarding the prefixed names, you may store your dictionaries in a proplist:
I didn't test the code, this is just t give you an idea about what I'm suggesting.