Converting hashtable to array of strings

后端 未结 5 2079
一整个雨季
一整个雨季 2021-02-18 23:33

How can I convert a hashtable to an array of strings? Suppose $l_table is a hashtable. If I try

$l_array = $l_table | format-table

then $l_arra

5条回答
  •  花落未央
    2021-02-19 00:00

    This is a common problem; Hashtable must be retrieved by either keys or values but you can't get a collection of pairs - that is what the original hashtable is, after all. In my example, the hashtable is represented by a "Dictionary" (you will remember from VBS :-). However you will need to determine a separator to delimit keys & values, in this case "Key<->Value"

    $ARRAY = ( $DICT.Keys | foreach-object { "$_<->$($DICT[$_])"})
    

    You can even sort the keys first if you want. In this case I reversed to Value<--->Key and sorted by values. I've also expanded the string a little to make more verbose but legible:

    $ARRAY = ( $DICT.Keys | foreach-object { $DICT[$_] + "<--->" + $_} | sort-object)
    

提交回复
热议问题