arrays - Quick File to Hashtable in PowerShell -
given array of key-value pairs (for example read in through convertfrom-stringdata), there streamlined way of turning hashtable or similar allow quick lookup? i.e. way not requiring me loop through array , manually build hashtable myself.
example data
10.0.0.1=alice.example.com 10.0.0.2=bob.example.com
example usage
$names = gc .\data.txt | convertfrom-stringdata // $names object[] $map = ? // $map should hashtable or equivalent echo $map['10.0.0.2'] // output should bob.example.com
basically i'm looking a, preferably, built-in file-to-hashtable function. or array-to-hashtable function.
note: @mjolnior explained, got hash tables, array of single value ones. fixed reading file -raw
, hence didn't require array hashtable conversion. updated question title match that.
convertfrom-stringdata create hash table.
you need give key-value pairs single multi-line string (not string array)
$map = get-content -raw .\data.txt | convertfrom-stringdata $map['10.0.0.2'] bob.example.com
when use get-content without -raw switch, you're giving convertfrom-stringdata array of single-line strings, , it's giving array of single-element hash tables:
$map = get-content .\data.txt | convertfrom-stringdata $map.gettype() $map[0].gettype() $map[0] ispublic isserial name basetype -------- -------- ---- -------- true true object[] system.array true true hashtable system.object key : 10.0.0.1 value : alice.example.com name : 10.0.0.1
Comments
Post a Comment