将字典添加到列表中。在函数:$user_list.add(“user3”, $user_name)中,我必须输入“USER3”。如何将变量传递到这个位置?
$user_list = @{
user1 = [ordered]@{
properties = ("666-555-2345", "1234 E Main St", "Dodge Charger");
misc_data = 34145}
user2 = [ordered]@{
properties = ("666-555-1234", "5678 N Elm St", "Plymouth Dart");
misc_data = 46112}
}
function add_new_user($user_name, $info, $misc){
$user_name = [ordered]@{
properties = $info;
misc_data = $misc}
$user_list.add(**"user3"**, $user_name)
}
add_new_user user3 ("666-555-1357", "9876 S Oak Rd", "Chevy PT Cruiser") 33879
$user_list
注意:这是一个函数,因此为每个新用户实际键入“User#”(在函数内)是不切实际的。
我不知道如何让它采用传递的变量作为 .ADD 的第一个值。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您可以从函数中更新您的哈希表,如下所示:
我添加了一个条件来检查用户是否已经存在,这样就不用替换键/值对了将返回用户已存在的消息。
如果您测试它,您会看到以下内容:
You can update your hash table from your function like this:
I have added a condition to check if the user is already there so that instead of replacing the key / value pair it would return the message that the user already exists.
If you test it you would see the following: