Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:35:11+00:00 2026-06-17T15:35:11+00:00

When creating a hash with an array key, How do i generate a key

  • 0

When creating a hash with an array key, How do i generate a key to look up the hash value.
that is, without getting it from the hash’s enumerator

$a = @{"a" = "1"
        "b" = "2"
        ("c","c1") = "3"}

Using a regular array, doesn’t seem to work.

$k1 = @("c","c1")
$a.ContainsKey($k1)  #false

However, if the array object is used on creation, this seems to work.

$k1 = @("c","c1")
$a = @{"a" = "1"
     "b" = "2"
 $k1 = "3"}
$a.ContainsKey($k1) #true

if for example, i use this to generate a hashtable:

$a = Get-Eventlog system -newest 100 | Group-Object {$_.EntryType, $_.Source } -AsHashTable

how do i generate a variable usable for key lookup myself?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:35:12+00:00Added an answer on June 17, 2026 at 3:35 pm

    Does the modification below help? It makes the keys into strings.

    $a = Get-Eventlog system -newest 100 | Group-Object {  
        $_.EntryType, $_.Source  
    } -AsHashTable -AsString
    

    Maybe you could give it a try.


    Update to answer, an explanation why you couldn’t use arrays for hashing the way you wanted, but strings work.

    Basically, there are two things you need to know.

    1. .NET (actually, really CLR) has the concepts of things with value semantics versus things with reference semantics. Value semantics are generally used for things with simple values, such as strings and numbers, "xyz", and 167 (arbitrary examples). Reference semantics are used for objects. Things with value semantics are held to be equal when their values are the same. Things with reference semantics are not equal unless they are the exact same object (located at the same address in memory).
      One additional wrinkle: things with values semantics can be represented by objects (this can involve something called boxing and unboxing, but I only throw those in there to point you at future exploration–it’s too much to get into for now). When objects are used to represent things with value semantics, the base class (actually a struct, I think) used is System.ValueType, and comparing two items of System.ValueType is a special case: even if the two items are not at the same memory address, if the two objects contain equal values, they are held to be “equal”.
      Take a look at the example below (comparing two ints vs two arrays), which illustrates these things. See how the ints are “equal” and the arrays are not.

      $a = 167; $b = 167; echo $($a.Equals($b)); #prints True
      $c = @(167,"xyz"); $d = @(167,"xyz"); echo $($c.Equals($d)); #prints False

    2. Any .NET (well, really, CLR) object is capable of having a hash code value computed on it, and that value is what is used if you are using the object as a hash key. The function GetHashCode() will produce a hash code for the item. Example: $a = "xyz"; $a.GetHashCode();

    The explanation for what you ran into

    Let’s put 1 and 2 above together with respect to your question. Since an array has reference semantics, comparing two array objects is comparing two different objects at two different memory locations, and they are not held to be equal. Further, this means that their hash codes will not be equal.

    This, using the arrays from above, echo $c.GetHashCode(); echo $d.GetHashCode(); produces two different hash codes.

    However, things with value semantics, like two value-identical strings, will actually produce the same hash code:
    $e = "xyz"; $f = "xyz"; echo $e.GetHashCode(); echo $f.GetHashCode();

    So, hashing by arrays caused you problems because the hash codes generated for the keys were different (unless you used the exact same array, something you correctly observed).
    But hashing by, say, strings, gets around the problem, because the hash codes are the same.

    Final Note

    You don’t need to know this to get the substance of the explanation above, but one of the things the CLR will do for you (generally) is actually make two value-identical value types, be the same object (or struct) when you are representing value types via objects. So $a = "xyz"; $b = "xyz"; will actually be made to refer to the same object, i.e., $a is the same object as $b–they are not just value identical, they are identical. This additional information does not need to clash with the simplified information above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a ruby hash that contains key/value pairs for creating a
So I am creating a Hash Table that uses an Array of Linked Lists
Is there a hash implementation around that doens't remember key values? I have to
I'm creating a object of hash in order to write a little script that
So I have a for loop thats creating a hash or array depending on
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a simple RPG game, first time using XNA. Trying to get my character
I need a Dictionary whose key is an array of integers for example Dictionary<int[],string>
i am having difficulties creating my own Zend_Auth_Adapter. i am using Doctrine 2 with
If I'm turning a ruby hash into a string of name-value pairs (to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.