Wednesday, 7 December 2016

Powershell adding an array of different types into an array

In powershell, I am trying to add different values into an array. I am grabbing one of the values thats an int from an Array. The rest are string values. I tried + , and add( ) . Is it because they are different values. How can I add different values to an Array?
    #set up values
    $dataIdListNameNonSpecial = @{}
    $email_general = "myEmail@gmail.com"
    $name_general ="John Smith"
    $numArray = 123 , 222 ,333

    #set up temp array
    $tempArray = $numArray[ 0 ], $email_general,  $name_general

    #try to add into array
    $dataIdListNameNonSpecial += , $tempArray 

    #try to add diffent way into array
    $dataIdListNameNonSpecial.Add( $tempArray)

----------------------------------------------------------------------------------------------------------------------

Best Answer;



@{} creates a hash table, not an array. Use @() instead, and use += to add to the array.




No comments:

Post a Comment