Discussion:
Case-sensitive Keys and Strings
(too old to reply)
tojo2000
2008-09-01 10:17:29 UTC
Permalink
There are two cases in which it seems like PowerShell's treatment of
strings as case-insensitive could cause problems. The first is the
"string1 -eq string2" case where you want to compare two strings and
find out if they are really the same. (Yes, I know you can use
CompareTo, but -eq seems more....PowerShelly.

The second thing is when it comes to hashtable keys. A lot of the use
cases for hashtables rely on assigning a key for a unique string. The
PowerShell implementation of a hashtable uses case-insensitive keys,
which can occasionally cause issues.


The second issue is more important than the first because there
doesn't appear to be any workaround, so my most important question is
this:

Is there a .NET object that implements IDictionary that uses case-
insensitive keys? Alternatively, is there another workaround?
unknown
2008-09-01 14:56:08 UTC
Permalink
Post by tojo2000
There are two cases in which it seems like PowerShell's treatment of
strings as case-insensitive could cause problems. The first is the
"string1 -eq string2" case where you want to compare two strings and
find out if they are really the same. (Yes, I know you can use
CompareTo, but -eq seems more....PowerShelly.
This one is _really_ easy, because there were endless arguments about it
during the beta. Although it seemed that case-agnostic behavior was best in
general, caseful comparison was critical enough that each and every
comparison operator has both caseful and caseless variants. The default is
caseless, which can be made explicit with a prepended letter 'i' (for
_I_nsensitive or _I_gnoring case) and 'c' for Caseful.

PS> "s" -ieq "S"
True
PS> "s" -ceq "S"
False
Post by tojo2000
The second thing is when it comes to hashtable keys. A lot of the use
cases for hashtables rely on assigning a key for a unique string. The
PowerShell implementation of a hashtable uses case-insensitive keys,
which can occasionally cause issues.
You can use any available comparer when initializing a hashtable. To see the
comparers, do this:

[stringcomparer] | gm -static -membertype property

Here's an example of using the CurrentCulture comparison type:

PS> $sscc = [system.stringcomparer]::CurrentCulture
PS> $ht = new-object system.collections.hashtable $sscc
PS> $ht["a"] = "b"
PS> $ht["A"] = "b"
PS> $ht

Name Value
---- -----
a b
A b
Post by tojo2000
The second issue is more important than the first because there
doesn't appear to be any workaround, so my most important question is
Is there a .NET object that implements IDictionary that uses case-
insensitive keys? Alternatively, is there another workaround?
tojo2000
2008-09-02 00:23:51 UTC
Permalink
Post by unknown
Post by tojo2000
There are two cases in which it seems like PowerShell's treatment of
strings as case-insensitive could cause problems.  The first is the
"string1 -eq string2" case where you want to compare two strings and
find out if they are really the same.  (Yes, I know you can use
CompareTo, but -eq seems more....PowerShelly.
This one is _really_ easy, because there were endless arguments about it
during the beta. Although it seemed that case-agnostic behavior was best in
general, caseful comparison was critical enough that each and every
comparison operator has both caseful and caseless variants. The default is
caseless, which can be made explicit with a prepended letter 'i' (for
_I_nsensitive or _I_gnoring case) and 'c' for Caseful.
PS> "s" -ieq "S"
True
PS> "s" -ceq "S"
False
D'oh! I totally forgot about that.
Post by unknown
Post by tojo2000
The second thing is when it comes to hashtable keys.  A lot of the use
cases for hashtables rely on assigning a key for a unique string.  The
PowerShell implementation of a hashtable uses case-insensitive keys,
which can occasionally cause issues.
You can use any available comparer when initializing a hashtable. To see the
[stringcomparer] | gm -static -membertype property
PS> $sscc = [system.stringcomparer]::CurrentCulture
PS> $ht = new-object system.collections.hashtable $sscc
PS> $ht["a"] = "b"
PS> $ht["A"] = "b"
PS> $ht
Name                           Value
----                           -----
a                              b
A                              b
Post by tojo2000
The second issue is more important than the first because there
doesn't appear to be any workaround, so my most important question is
Is there a .NET object that implements IDictionary that uses case-
insensitive keys?  Alternatively, is there another workaround?
Awesome! Thanks.

Loading...