Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Building up Hash Tables avoiding key name conflicts

$
0
0

Hello All

Can someone please assist me with the following question :)
In the past when creating hash tables I have mistakenly specified the same key name and therefore get an error saying that key name already exists in the hash table. Therefore I was trying to think of a way to take data from a pipeline with may contain duplicate values and create a hash table but automatically skip duplicates as they arise (using PosH 2 at the moment)

I came up with the following solution (do not worry about the values at this time, just keeping it simply and thinking about the keys)

My question is, is there an easier/more elegant way to achieve this, 

Thanks all

Ernie

cls
$HT= @{"John Smith"= 0;"Jane Brown"= 0}
$X="Ernest Brant","Bert Jackson","Jack Brown","Jane Brown","Ernest Brant","Bert Jackson"

foreach ($elementin$X)

{

if (($HT.Keys | % {$_.ToString()}) -match$element) {continue} else

{$HT.Add($element,0)}


}

$HT

Viewing all articles
Browse latest Browse all 8583

Latest Images

Trending Articles



Latest Images