Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Comparing strings - is it a bug?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 03-11-2008   #1 (permalink)
Tibor Soos
Guest


 

Comparing strings - is it a bug?

My computer has Hungarian regional setting, with the Hungarian default sort
order. The official Hungarian alphabet is: a á b c d e é f g ...

When I compare strings in PowerShell I receive this:

[91] PS I:\>"á" -gt "a"
True
[92] PS I:\>"áb" -gt "ab"
True
[93] PS I:\>"ác" -gt "ab"
True
[94] PS I:\>"áb" -gt "ac"
False

I don't understand the last one! If "á" is greater than "a" (that's OK),
then why "áb" is not greater than "ac"??????? Is it a bug?
--
Tibor

My System SpecsSystem Spec
Old 03-12-2008   #2 (permalink)
Shay Levi
Guest


 

Re: Comparing strings - is it a bug?


ASAIK, PowerShell converts the characters to their numeric values and then
compares the two. Take into count that the comparison is not case-sensitive.
Find the characters value, it may give you a hint:

([int[]][char[]]"áb" | measure-object -sum).sum

([int[]][char[]]"ac" | measure-object -sum).sum


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> My computer has Hungarian regional setting, with the Hungarian default
> sort order. The official Hungarian alphabet is: a á b c d e é f g ...
>
> When I compare strings in PowerShell I receive this:
>
> [91] PS I:\>"á" -gt "a"
> True
> [92] PS I:\>"áb" -gt "ab"
> True
> [93] PS I:\>"ác" -gt "ab"
> True
> [94] PS I:\>"áb" -gt "ac"
> False
> I don't understand the last one! If "á" is greater than "a" (that's
> OK), then why "áb" is not greater than "ac"??????? Is it a bug?
>

My System SpecsSystem Spec
Old 03-12-2008   #3 (permalink)
Tibor Soos
Guest


 

Re: Comparing strings - is it a bug?

The result:

[1] PS I:\>([int[]][char[]]"áb" | measure-object -sum).sum
323
[2] PS I:\>([int[]][char[]]"ac" | measure-object -sum).sum
196

Anyway, I don't think PS works the way you described... It's not that easy,
because it should take into account the different sort orders of different
cultures.

Tibor
Quote:

> ASAIK, PowerShell converts the characters to their numeric values and then
> compares the two. Take into count that the comparison is not case-sensitive.
> Find the characters value, it may give you a hint:
>
> ([int[]][char[]]"áb" | measure-object -sum).sum
>
> ([int[]][char[]]"ac" | measure-object -sum).sum
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > My computer has Hungarian regional setting, with the Hungarian default
> > sort order. The official Hungarian alphabet is: a á b c d e é f g ...
> >
> > When I compare strings in PowerShell I receive this:
> >
> > [91] PS I:\>"á" -gt "a"
> > True
> > [92] PS I:\>"áb" -gt "ab"
> > True
> > [93] PS I:\>"ác" -gt "ab"
> > True
> > [94] PS I:\>"áb" -gt "ac"
> > False
> > I don't understand the last one! If "á" is greater than "a" (that's
> > OK), then why "áb" is not greater than "ac"??????? Is it a bug?
> >
My System SpecsSystem Spec
Old 03-12-2008   #4 (permalink)
Shay Levi
Guest


 

Re: Comparing strings - is it a bug?


Hi

The results from the below culture-aware script are both true on my machine,
can you check yours?

##############

$huCculture = "hu-HU"

[scriptblock] $script = {
$ab = ([int[]][char[]]"áb" | measure-object -sum).sum
$ac = ([int[]][char[]]"ac" | measure-object -sum).sum
$ab -gt $ac
"áb" -gt "ac"
}

$oldCulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture

[System.Threading.Thread]::CurrentThread.CurrentUICulture = $huCculture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $huCculture

& $script

[System.Threading.Thread]::CurrentThread.CurrentUICulture = $oldCulture
[System.Threading.Thread]::CurrentThread.CurrentCulture = $oldCulture

#################



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> The result:
>
> [1] PS I:\>([int[]][char[]]"áb" | measure-object -sum).sum
> 323
> [2] PS I:\>([int[]][char[]]"ac" | measure-object -sum).sum
> 196
> Anyway, I don't think PS works the way you described... It's not that
> easy, because it should take into account the different sort orders of
> different cultures.
>
> Tibor
>
Quote:

>> ASAIK, PowerShell converts the characters to their numeric values and
>> then compares the two. Take into count that the comparison is not
>> case-sensitive. Find the characters value, it may give you a hint:
>>
>> ([int[]][char[]]"áb" | measure-object -sum).sum
>>
>> ([int[]][char[]]"ac" | measure-object -sum).sum
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
Quote:

>>> My computer has Hungarian regional setting, with the Hungarian
>>> default sort order. The official Hungarian alphabet is: a á b c d e
>>> é f g ...
>>>
>>> When I compare strings in PowerShell I receive this:
>>>
>>> [91] PS I:\>"á" -gt "a"
>>> True
>>> [92] PS I:\>"áb" -gt "ab"
>>> True
>>> [93] PS I:\>"ác" -gt "ab"
>>> True
>>> [94] PS I:\>"áb" -gt "ac"
>>> False
>>> I don't understand the last one! If "á" is greater than "a" (that's
>>> OK), then why "áb" is not greater than "ac"??????? Is it a bug?

My System SpecsSystem Spec
Old 03-12-2008   #5 (permalink)
Shay Levi
Guest


 

Re: Comparing strings - is it a bug?

BTW, the numeric results I get:

PS > ([int[]][char[]]"áb" | measure-object -sum).sum
1587

PS > ([int[]][char[]]"ac" | measure-object -sum).sum
196

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Hi
>
> The results from the below culture-aware script are both true on my
> machine, can you check yours?
>
> ##############
>
> $huCculture = "hu-HU"
>
> [scriptblock] $script = {
> $ab = ([int[]][char[]]"áb" | measure-object -sum).sum
> $ac = ([int[]][char[]]"ac" | measure-object -sum).sum
> $ab -gt $ac
> "áb" -gt "ac"
> }
> $oldCulture =
> [System.Threading.Thread]::CurrentThread.CurrentUICulture
>
> [System.Threading.Thread]::CurrentThread.CurrentUICulture =
> $huCculture [System.Threading.Thread]::CurrentThread.CurrentCulture =
> $huCculture
>
> & $script
>
> [System.Threading.Thread]::CurrentThread.CurrentUICulture =
> $oldCulture [System.Threading.Thread]::CurrentThread.CurrentCulture =
> $oldCulture
>
> #################
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
Quote:

>> The result:
>>
>> [1] PS I:\>([int[]][char[]]"áb" | measure-object -sum).sum
>> 323
>> [2] PS I:\>([int[]][char[]]"ac" | measure-object -sum).sum
>> 196
>> Anyway, I don't think PS works the way you described... It's not that
>> easy, because it should take into account the different sort orders
>> of
>> different cultures.
>> Tibor
>>
Quote:

>>> ASAIK, PowerShell converts the characters to their numeric values
>>> and then compares the two. Take into count that the comparison is
>>> not case-sensitive. Find the characters value, it may give you a
>>> hint:
>>>
>>> ([int[]][char[]]"áb" | measure-object -sum).sum
>>>
>>> ([int[]][char[]]"ac" | measure-object -sum).sum
>>>
>>> -----
>>> Shay Levi
>>> $cript Fanatic
>>> http://scriptolog.blogspot.com
>>>> My computer has Hungarian regional setting, with the Hungarian
>>>> default sort order. The official Hungarian alphabet is: a á b c d e
>>>> é f g ...
>>>>
>>>> When I compare strings in PowerShell I receive this:
>>>>
>>>> [91] PS I:\>"á" -gt "a"
>>>> True
>>>> [92] PS I:\>"áb" -gt "ab"
>>>> True
>>>> [93] PS I:\>"ác" -gt "ab"
>>>> True
>>>> [94] PS I:\>"áb" -gt "ac"
>>>> False
>>>> I don't understand the last one! If "á" is greater than "a" (that's
>>>> OK), then why "áb" is not greater than "ac"??????? Is it a bug?

My System SpecsSystem Spec
Old 03-13-2008   #6 (permalink)
Tibor Soos
Guest


 

Re: Comparing strings - is it a bug?

My results for your script:

True
False

--
Tibor


"Shay Levi" wrote:
Quote:

>
> Hi
>
> The results from the below culture-aware script are both true on my machine,
> can you check yours?
>
> ##############
>
> $huCculture = "hu-HU"
>
> [scriptblock] $script = {
> $ab = ([int[]][char[]]"áb" | measure-object -sum).sum
> $ac = ([int[]][char[]]"ac" | measure-object -sum).sum
> $ab -gt $ac
> "áb" -gt "ac"
> }
>
> $oldCulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
>
> [System.Threading.Thread]::CurrentThread.CurrentUICulture = $huCculture
> [System.Threading.Thread]::CurrentThread.CurrentCulture = $huCculture
>
> & $script
>
> [System.Threading.Thread]::CurrentThread.CurrentUICulture = $oldCulture
> [System.Threading.Thread]::CurrentThread.CurrentCulture = $oldCulture
>
> #################
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
>
Quote:

> > The result:
> >
> > [1] PS I:\>([int[]][char[]]"áb" | measure-object -sum).sum
> > 323
> > [2] PS I:\>([int[]][char[]]"ac" | measure-object -sum).sum
> > 196
> > Anyway, I don't think PS works the way you described... It's not that
> > easy, because it should take into account the different sort orders of
> > different cultures.
> >
> > Tibor
> >
Quote:

> >> ASAIK, PowerShell converts the characters to their numeric values and
> >> then compares the two. Take into count that the comparison is not
> >> case-sensitive. Find the characters value, it may give you a hint:
> >>
> >> ([int[]][char[]]"áb" | measure-object -sum).sum
> >>
> >> ([int[]][char[]]"ac" | measure-object -sum).sum
> >>
> >> -----
> >> Shay Levi
> >> $cript Fanatic
> >> http://scriptolog.blogspot.com
> >>> My computer has Hungarian regional setting, with the Hungarian
> >>> default sort order. The official Hungarian alphabet is: a á b c d e
> >>> é f g ...
> >>>
> >>> When I compare strings in PowerShell I receive this:
> >>>
> >>> [91] PS I:\>"á" -gt "a"
> >>> True
> >>> [92] PS I:\>"áb" -gt "ab"
> >>> True
> >>> [93] PS I:\>"ác" -gt "ab"
> >>> True
> >>> [94] PS I:\>"áb" -gt "ac"
> >>> False
> >>> I don't understand the last one! If "á" is greater than "a" (that's
> >>> OK), then why "áb" is not greater than "ac"??????? Is it a bug?
>
>
>
My System SpecsSystem Spec
Old 03-13-2008   #7 (permalink)
Shay Levi
Guest


 

Re: Comparing strings - is it a bug?


What are the values you get for $ab and $ac?

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> My results for your script:
>
> True
> False
> "Shay Levi" wrote:
>
Quote:

>> Hi
>>
>> The results from the below culture-aware script are both true on my
>> machine, can you check yours?
>>
>> ##############
>>
>> $huCculture = "hu-HU"
>>
>> [scriptblock] $script = {
>> $ab = ([int[]][char[]]"áb" | measure-object -sum).sum
>> $ac = ([int[]][char[]]"ac" | measure-object -sum).sum
>> $ab -gt $ac
>> "áb" -gt "ac"
>> }
>> $oldCulture =
>> [System.Threading.Thread]::CurrentThread.CurrentUICulture
>>
>> [System.Threading.Thread]::CurrentThread.CurrentUICulture =
>> $huCculture [System.Threading.Thread]::CurrentThread.CurrentCulture =
>> $huCculture
>>
>> & $script
>>
>> [System.Threading.Thread]::CurrentThread.CurrentUICulture =
>> $oldCulture [System.Threading.Thread]::CurrentThread.CurrentCulture =
>> $oldCulture
>>
>> #################
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
Quote:

>>> The result:
>>>
>>> [1] PS I:\>([int[]][char[]]"áb" | measure-object -sum).sum
>>> 323
>>> [2] PS I:\>([int[]][char[]]"ac" | measure-object -sum).sum
>>> 196
>>> Anyway, I don't think PS works the way you described... It's not
>>> that
>>> easy, because it should take into account the different sort orders
>>> of
>>> different cultures.
>>> Tibor
>>>
>>>> ASAIK, PowerShell converts the characters to their numeric values
>>>> and then compares the two. Take into count that the comparison is
>>>> not case-sensitive. Find the characters value, it may give you a
>>>> hint:
>>>>
>>>> ([int[]][char[]]"áb" | measure-object -sum).sum
>>>>
>>>> ([int[]][char[]]"ac" | measure-object -sum).sum
>>>>
>>>> -----
>>>> Shay Levi
>>>> $cript Fanatic
>>>> http://scriptolog.blogspot.com
>>>>> My computer has Hungarian regional setting, with the Hungarian
>>>>> default sort order. The official Hungarian alphabet is: a á b c d
>>>>> e é f g ...
>>>>>
>>>>> When I compare strings in PowerShell I receive this:
>>>>>
>>>>> [91] PS I:\>"á" -gt "a"
>>>>> True
>>>>> [92] PS I:\>"áb" -gt "ab"
>>>>> True
>>>>> [93] PS I:\>"ác" -gt "ab"
>>>>> True
>>>>> [94] PS I:\>"áb" -gt "ac"
>>>>> False
>>>>> I don't understand the last one! If "á" is greater than "a"
>>>>> (that's
>>>>> OK), then why "áb" is not greater than "ac"??????? Is it a bug?

My System SpecsSystem Spec
Old 03-13-2008   #8 (permalink)
Kiron
Guest


 

Re: Comparing strings - is it a bug?

When both operands are strings PowerShell compares strings as strings, alphabetically:

# true because á's position in the alphabet is greater than a's
'á'-gt'a'

# true, same as before; the next element in both operands is the same
'áb'-gt'ab'

# true, á's position in the alphabet is greater than a's
# and so is c's position regarding b's
'ác'-gt'ab'

# false, even though á's position is greater than a's
# b's position is lower than c's; the string 'áb' comes
# before -or is less than- 'ac' when sorted alphabetically
'áb'-gt'ac'

# sorting the strings demonstrates it
'á','a' | sort
'áb','ab' | sort
'ác','ab'| sort
'áb','ac'| sort

# compare numbers as strings
# true, obviously
'13'-gt'12'

# sort numbers as strings
'13','12'|sort

# false, but 123 is greater than 13
# the thing is that they are strings not integers
'123'-gt'13'

# sort numbers as strings
'123','13'|sort

--
Kiron
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two lists KeithK PowerShell 4 07-09-2008 05:27 PM
comparing hashtable values Cookiecutter PowerShell 4 05-20-2008 10:36 AM
comparing secure strings... Ben Christian PowerShell 3 04-02-2008 03:42 PM
comparing dates to $null Hal Rottenberg PowerShell 5 08-27-2007 11:49 AM
Comparing filenames to strings Tim PowerShell 17 08-24-2007 10:34 AM


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008