命名变量时添加参数

发布于 2024-10-04 16:19:03 字数 296 浏览 2 评论 0原文

我想为每个身份号码创建一个字典。

传入的参数可以是例如 {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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-10-11 16:19:03

你想在运行时构造一个变量名

这是不可能的,抱歉。

在 Erlang 中,您无法在运行时为事物附加名称。您唯一的选择是将所有字典存储在另一个字典中并传递它。

为了将静态标识符 (D) 和数字粘合在一起,Erlang 为您提供了多种可能性。最简单的方法是构造一个元组 {d, ID} 并将其用作键。
您还可以使用下面描述的任何字符串连接方法。

现在你已经澄清了你的问题,我原来的大部分解释都失去了背景。无论如何,我要把他们留在这里。

你想要连接一个字符串和一个整数

这很简单,只需使用:

"D" ++ integer_to_list(SomeID)

你想要在字典中设置一个值,使用上面的作为键

Erlang 是一种函数式语言,所以破坏性地修改字典是不可能的。
假设您有一个 字典 存储在变量 Dict 中并且 SomeID 设置为 3。
您可以使用以下方法获取 Dict 的副本,其中“D3”设置为 Value

NewDict = dict:store("D" ++ integer_to_list(SomeID), Value, Dict)

您正在寻找类似 printf 的机制

Erlang 函数 io_lib:format (FmtString, Args) 可以用于此目的。与 printf 不同,格式化指令以波形符字符开头。
您可以在手册中查找正确的指令,但是对于将一个整数添加到字符串 "D" 中,调用将如下所示:

io_lib:format("D~b", [SomeID])

问题io_lib:format/2 返回一个深层列表人物。如果您想要一个平面列表,请在结果上使用lists:flatten/1。你可以在 Erlang shell 中尝试一下:

(b@frog)1> SomeID = 3.
3                                   
(b@frog)2> io_lib:format("D~b", [SomeID]).
[68,"3"] 
(b@frog)3> lists:flatten(io_lib:format("D~b", [SomeID])).
"D3"


† 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:

"D" ++ integer_to_list(SomeID)

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 and SomeID is set to three.
You can obtain a copy of Dict in which "D3" is set to Value using:

NewDict = dict:store("D" ++ integer_to_list(SomeID), Value, Dict)

You are looking for a printf-like mechanism

The Erlang function io_lib:format(FmtString, Args) can be used for that. Unlike printf, 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:

io_lib:format("D~b", [SomeID])

Gotcha: io_lib:format/2 returns a deep list of characters. If you want a flat list, use lists:flatten/1 on the result. You can try this in the Erlang shell:

(b@frog)1> SomeID = 3.
3                                   
(b@frog)2> io_lib:format("D~b", [SomeID]).
[68,"3"] 
(b@frog)3> lists:flatten(io_lib:format("D~b", [SomeID])).
"D3"


† 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).

初心 2024-10-11 16:19:03

Erlang 中的 dict 结构不允许您为其命名。而且 dict 中的名字可能毫无意义。 dict 只是一个键/值字典,其表示形式未定义。

如果您想要引用多个字典,那么将它们存储在记录或元组中以“命名”它们可能是个好主意。您不应该从函数参数中创建变量名。

{my_dict, Dict}

#state{
  my_dict = Dict
}

假设您需要将字典名称设置为 d_mycustomname,您可以这样写:

atomize(Name) ->
  list_to_atom("d_" ++ Name).

作为其他受访者,我不确定这是否正是您所要求的。请重新表述您的问题以获得更好的答案。我现在的回答只是酒精驱动下的猜测。

更新后:

关于向字典添加值的方式,您想要的可能是更新而不是存储操作。

enqueue(D, {_Id, Key, Value}) ->
    Update = fun (Old) -> Old ++ [Value] end,
    dict:update(Key, Update, [Value], D).

这会将您的值附加到“队列”中。如果尚不存在,它将创建一个。

关于前缀名称,您可以将字典存储在 proplist 中:

enqueue(ListOfDicts, {Id, Key, Value}) ->
    Name = "dict_" ++ Id,
    case proplists:get_value(Name, ListOfDicts) of
      undefined -> % No such a dict yet
        [{Name, dict:new()}|ListOfDicts];
      D ->
        Update = fun (Old) -> Old ++ [Value] end,
        NewD = dict:update(Key, Update, [Value], D),
        lists:keyreplace(Name, 1, ListOfDicts, {Name, NewD})
    end.

我没有测试代码,这只是让您了解我的建议。

The dict structure in Erlang doesn't allow you to give it a name. And it's probably pointless to have a name in a dict. A dict 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.

{my_dict, Dict}

#state{
  my_dict = Dict
}

Supposing you need to have the dictionary names as d_mycustomname, you could write something like:

atomize(Name) ->
  list_to_atom("d_" ++ Name).

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 a store operation.

enqueue(D, {_Id, Key, Value}) ->
    Update = fun (Old) -> Old ++ [Value] end,
    dict:update(Key, Update, [Value], D).

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:

enqueue(ListOfDicts, {Id, Key, Value}) ->
    Name = "dict_" ++ Id,
    case proplists:get_value(Name, ListOfDicts) of
      undefined -> % No such a dict yet
        [{Name, dict:new()}|ListOfDicts];
      D ->
        Update = fun (Old) -> Old ++ [Value] end,
        NewD = dict:update(Key, Update, [Value], D),
        lists:keyreplace(Name, 1, ListOfDicts, {Name, NewD})
    end.

I didn't test the code, this is just t give you an idea about what I'm suggesting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文