数组/对象键在PowerShell中哈希表 [英] array/object keys for hashtables in powershell

查看:327
本文介绍了数组/对象键在PowerShell中哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建一个数组的关键,如何生成一个密钥来查找哈希值的哈希值。 也就是说,不从哈希的枚举得到它

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?

推荐答案

下面是否帮助修改?它使按键为字符串。

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(实际上,真正 CLR )拥有的事物的概念与< STRONG>值语义对事情的引用语义。值语义通常用于事情用简单的值,如字符串和数字,XYZ 167 (任意实施例)。引用语义用于对象。 东西值语义保持相等时,其值是相同东西引用语义是不相等的,除非它们是完全相同的对象(位于内存中的地址相同)。
    一个额外的皱纹:东西值语义可以重新$ P $的物体psented(这可能涉及到一些所谓的装箱和拆箱,但我只能扔那些在里面点你在未来的探索 - 它是过多地进入现在)。当对象被用来重新present东西值语义,基类(实际上是一个结构,我认为)用的是 System.ValueType ,并比较两个项目 System.ValueType 是一个特殊情况:即使这两个项目不是在同一个内存地址,如果两个对象包含相同的值,它们被认为是平等 。照片 看看下面的例子(比较两个整数VS两个阵列),这说明了这些东西。见整数如何平等和数组都没有。

  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;回声$($ a.Equals($ B)); #prints真
 $ C = @(167,XYZ); $ D = @(167,XYZ);回声$($ c.Equals($ D)); #prints假

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

任何.NET(好,真的,CLR)对象能够有计算上有一个散列code值,并且该值是什么,如果你正在使用的对象作为一个哈希键使用。该功能GetHash code()会产生一个散列$ C $下的项目。例如: $ a =XYZ; $ a.GetHash code();

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();

为你跑进了解释

让我们把1和2以及关于你的问题。因为一个阵列具有引用语义,比较两个阵列的对象是比较两个不同的对象在两个不同的存储器位置,并且它们不认为是相等的。此外,这意味着的散列codeS将不等于

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.

本,使用数组上面,回声$ c.GetHash code();回声$ d.GetHash code(); 产生两个不同的散codeS

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

然而,事情值语义,就像两个值相同的字符串,实际上会产生相同的哈希值code: $ E =XYZ; $ F =XYZ;回声$ e.GetHash code();回声$ f.GetHash code();

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();

因此​​,通过阵列散列给您造成问题,因为散列codeS生成的密钥是不同的(除非您使用的是完全相同的数组,是你正确的观察)。   但是散列由,比方说,串,得到解决该问题,因为散列codeS是相同的。

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.

最后请注意

您并不需要知道这一点得到了上述说明的实质内容,但对事情的CLR会为你做(一般),一种是实际做两个值相同的值类型,同一个对象(或结构),当你通过对象重新presenting值类型。因此, $ a =XYZ; $ B =XYZ; 将实际做出指代相同的对象,即$ a是相同的对象$ b的 - 它们不只是重视相同的,它们是相同的。该附加信息并不需要冲突与上述简易信息。

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.

这篇关于数组/对象键在PowerShell中哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆