添加词典到列表。在函数中:$user_list.add("user3", $user_name),我必须输入 "USER3"。如何将变量传递给这个点?

ADD Dictionary to List. In Funtion: $user_list.add("user3", $user_name), I must type in "USER3". How to pass variable to this spot?

$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 的第一个值获取传递的变量。

$user_list.add("user3", $user_name) =

$user_list.add($user_name, $user_name) =

您可以像这样从您的函数中更新您的 哈希 table

$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) {
    if($user_list.ContainsKey($user_name)) {
        return "$user_name already exists in `$userList"
    }
    $user_list[$user_name] = [ordered]@{
        properties = $info
        misc_data = $misc
    }
}

我添加了一个条件来检查用户是否已经存在,这样就不会替换 键/值 对,而是 return 用户的消息已经存在。

如果您对其进行测试,您会看到以下内容:

PS /> add_new_user user3 ("666-555-1357", "9876 S Oak Rd", "Chevy PT Cruiser") 33879
PS /> $user_list

Name                           Value
----                           -----
user1                          {properties, misc_data}
user3                          {properties, misc_data}
user2                          {properties, misc_data}

PS /> add_new_user user2 ("666-555-1357", "9876 S Oak Rd", "Chevy PT Cruiser") 33879

user2 already exists in $userList