{"id":1249,"date":"2013-04-09T09:30:41","date_gmt":"2013-04-09T13:30:41","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=1249"},"modified":"2013-04-08T09:08:58","modified_gmt":"2013-04-08T13:08:58","slug":"hashtables-item-has-already-been-added","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/hashtables-item-has-already-been-added\/","title":{"rendered":"Hashtables: Item has already been added"},"content":{"rendered":"<p>In my previous two posts about hashtables I provided examples for creating a hashtable, returning a specific value and a description around performance.  This post will focus on the errors you should expect if you start using a hashtable.<\/p>\n<p>The most common exception I see happens when the data I&#8217;m storing in the hashtable is not unique.  For example, in the first post in the series we created a simple hashtable from a list of HTML colors.  If my input list had the color &#8216;red&#8217; listed on two different lines, I would have seen an error similar to this:<\/p>\n<p><font color='red'>Add : Exception calling &#8220;Add&#8221; with &#8220;2&#8221; argument(s): &#8220;Item has already been added. Key in dictionary: &#8216;Red&#8217;  Key being<br \/>\n added: &#8216;Red'&#8221;<br \/>\nAt line:1 char:17<br \/>\n+ $colorCodeHT.Add <<<< ('Red','123456')\n    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException\n    + FullyQualifiedErrorId : DotNetMethodException\n<\/font><\/p>\n<p>This is an easy error message to generate if you want to see the actual error.  Follow the examples in the first post and then try and re-add the color red using the following line:<\/p>\n<pre><code class=\"language-powershell\">\r\n$colorCodeHT.Add('Red','123456')\r\n<\/code><\/pre>\n<p>The hashtable object includes a .Contains method, which can be used in an if statement to prevent this exception from happening:<\/p>\n<pre><code class=\"language-powershell\">if ($colorCodeHT.Contains('Red')) { \"Color 'red' has already been added\" } else { $colorCodeHT.Add('Red','123456') }<\/code><\/pre>\n<p>This statement checks for the presence of a &#8216;Red&#8217; item and only attempts to add if a value does not already exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous two posts about hashtables I provided examples for creating a hashtable, returning a specific value and a description around performance. This post will focus on the errors you should expect if you start using a hashtable. The &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/hashtables-item-has-already-been-added\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1249","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=1249"}],"version-history":[{"count":4,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1249\/revisions"}],"predecessor-version":[{"id":1266,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/1249\/revisions\/1266"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}