Sign in to follow this  
Followers 0
blindwig

Case sensative string comparison

12 posts in this topic

I know that "$sA = $sB" will return a case insensative string comparison, and "$sA == $s" will return a case sensative string comparison.

But >, >=, <, and <= always do a case insensative comparison. Is there an option/operator that will do a case sensative comparison?

Share this post


Link to post
Share on other sites



I know that "$sA = $sB" will return a case insensative string comparison, and "$sA == $s" will return a case sensative string comparison.

But >, >=, <, and <= always do a case insensative comparison.  Is there an option/operator that will do a case sensative comparison?

<{POST_SNAPBACK}>

If it is any thing other than a string then you dont have Case Sensitivity. If it is a string then there are no > (greater than) or < (less than) comparison's you can do to a string. So using == is pretty much the only way to see if strings are the exact same.

A side step would be to create a UDF that will go through and convert all the Chars to their ASCII value and then compare the numbers sand see which is greater. You could add the ASCII values together I would think would be the more proper way.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

I'm pretty sure this will do what you want

StringInStr ( "string", "substring" [, casesense [, occurance]] )

just an extra step... hope it helps

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

@JSThePatriot:

Yes, I know that == is case-sensative and that = is case-insensative. Read my original post.

Yes I could write a UDF to compare the strings 1 character at a time, but I'm wondering if there is a way to use built-in options. No, adding the values together would quickly give you a number that would be too big for AutoIt to handle.

@Valuater:

I don't see how that helps... Explain more please?

Here's some examples of how AutoIt compares strings:

'A'='a' is true, obviously case insensative

'A'=='a' is false, obviously case sensative

'A'<'a' is false

'a'<'A' is false

'A'<'b' is true

'A'<'B' is true

'a'<'b' is true

'a'<'B' is true

Share this post


Link to post
Share on other sites

Yes I know you said that in your first post. I was confirming what you said, and was wondering why you would need the < or > comparison operator. I now see below what you are trying to do. I will think it over a bit more.

As far as using Valuater's idea it would be something like this.

$sA = "A"
$sa = "a"

If StringInStr($sA, $sa, 1) Then
   ;Do something here
EndIf

You may have to convert your string into single characters for that to work, and have an array of characters for checking against.

Could you explain a bit more why you would need to compare that?

JS

@JSThePatriot:

Yes, I know that == is case-sensative and that = is case-insensative.  Read my original post.

Yes I could write a UDF to compare the strings 1 character at a time, but I'm wondering if there is a way to use built-in options.  No, adding the values together would quickly give you a number that would be too big for AutoIt to handle.

@Valuater:

I don't see how that helps...  Explain more please?

Here's some examples of how AutoIt compares strings:

'A'='a' is true, obviously case insensative

'A'=='a' is false, obviously case sensative

'A'<'a' is false

'a'<'A' is false

'A'<'b' is true

'A'<'B' is true

'a'<'b' is true

'a'<'B' is true

<{POST_SNAPBACK}>


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Could you use the Asc() function. Something like

$split = StringSplit($msg, "")

$total = 0

For $i = 1 to $split[0]

$addchar = Asc($split[$i])

$total = $total + $addchar

Next

idk if that would work but you could put that as a function. For example:

$xsMsg = _exactcompare($sMsg)

$xiMsg = _exactcompare($iMsg)

If $xsmsg <= $xiMsg Then

BLAH

EndIf

Func _exactcompare($msg)

Local $split = StringSplit($msg, "")

Local $total = 0

For $i = 1 to $split[0]

$addchar = Asc($split[$i])

$total = $total + $addchar

Next

Return $total

EndFunc

Prolly has errors in it but you get the idea. I don't have autoit on this computer so i can't really test it. Good luck


My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites

Could you use the Asc() function. Something like

$split = StringSplit($msg, "")

$total = 0

For $i = 1 to $split[0]

$addchar = Asc($split[$i])

$total = $total + $addchar

Next

That wouldn't work because the string 'AB' would give the same value as the string 'BA', and the string 'yz' would have the same value as the string 'ABp', for example.

The only what that would work is to:

$total = $total * 256 + $AddChar

but as I said before, that would quickly exceed the precision of AutoIt numbers.

The only way I can think of is to write a UDF that compares each string character by character, which wouldn't be hard to do, I was just wondering if there was a case-sensative alternative to >, like == is an alternative to =

If not, I'll just write a function, no big deal.

Share this post


Link to post
Share on other sites

As far as using Valuater's idea it would be something like this.

$sA = "A"
$sa = "a"

If StringInStr($sA, $sa, 1) Then
  ;Do something here
EndIf
I still don't see how that would compare the strings, it would only check to see if the first one included the second one as a substring.

Could you explain a bit more why you would need to compare that?

For sorting a list of strings. Like _ArraySort does, but I wanted to add a case sense option to my routine.

Share this post


Link to post
Share on other sites

Obviously I am not the sharpest here.... nor do I completely understand what is within your $strings... BUT...

Here is a real effort for ya

#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>
#include <String.au3>

Dim $String_[4], $Test_[4]

$String_[1] = "dave"
$String_[2] = "MARCY"
$String_[3] = "Andy"

for $x = 1 to 3
$Test_[$x] = StringUpper ( $String_[$x] )


Next

If $test_[1] >= $test_[2] And $test_[1] >= $test_[3] And  $test_[2] >= $test_[3] Then
    $Final_1 = _StringProper ( $String_[1] )
    $Final_2 = _StringProper ( $String_[2] )
    $Final_3 = _StringProper ( $String_[3] )
EndIf

If $test_[1] >= $test_[2] And $test_[1] >= $test_[3] And  $test_[3] >= $test_[2] Then
    $Final_1 = _StringProper ( $String_[1] )
    $Final_2 = _StringProper ( $String_[3] )
    $Final_3 = _StringProper ( $String_[2] )
EndIf

If $test_[2] >= $test_[1] And $test_[2] >= $test_[3] And  $test_[1] >= $test_[3] Then
    $Final_1 = _StringProper ( $String_[2] )
    $Final_2 = _StringProper ( $String_[1] )
    $Final_3 = _StringProper ( $String_[3] )
EndIf

If $test_[2] >= $test_[1] And $test_[2] >= $test_[3] And  $test_[3] >= $test_[1] Then
    $Final_1 = _StringProper ( $String_[2] )
    $Final_2 = _StringProper ( $String_[3] )
    $Final_3 = _StringProper ( $String_[1] )
EndIf

If $test_[3] >= $test_[1] And $test_[3] >= $test_[2] And  $test_[1] >= $test_[2] Then
    $Final_1 = _StringProper ( $String_[3] )
    $Final_2 = _StringProper ( $String_[1] )
    $Final_3 = _StringProper ( $String_[2] )
EndIf

If $test_[3] >= $test_[1] And $test_[3] >= $test_[2] And  $test_[2] >= $test_[1] Then
    $Final_1 = _StringProper ( $String_[3] )
    $Final_2 = _StringProper ( $String_[2] )
    $Final_3 = _StringProper ( $String_[1] )
EndIf

MsgBox(0,"Results", "List Order is   " & $Final_3 & ",  " & $Final_2 & ",  " & $Final_1 & "   ")

Hope it helps

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

@Valuater:

I'm not sure how you think that has anything to do with what I asked. Your code uses >= which I've already stated is not case sensative. Besides that, you convert all the strings to uppercase to compare them, which would destory any routine for case sensativity, and finally you run the output through StringProper which totally destroys the original case of the strings.

Here's the routine I wrote - it can compare numbers or strings, and has a flag for comparing case sensative strings:

CODE

;This function compares given variants $vA and $vB

;Returns 1 if $vA > $vB

;Returns 0 if $vA = $vB

;Returns -1 if $vA < $vB

Func _ValueCompare($vA, $vB, $CaseSense = 0)

;compare numbers

If IsNumber($vA) And IsNumber($vB) Then

If $vA > $vB Then Return 1

If $vA = $vB Then Return 0

Return -1

EndIf

;convert to strings

If Not IsString($vA) Then $vA = String($vA)

If Not IsString($vB) Then $vB = String($vB)

;compare strings

If $CaseSense Then ;Case sensative comparison

Local $i=0

While $i <= Stringlen($vA) and $i <= StringLen($vB)

If Asc(StringMid($vA, $i, 1)) > Asc(StringMid($vB, $i, 1)) Then Return 1

If Asc(StringMid($vA, $i, 1)) < Asc(StringMid($vB, $i, 1)) Then Return -1

$i = $i + 1

WEnd

If StringLen($vA) > StringLen($vB) Then Return 1

If StringLen($vA) = StringLen($vB) Then Return 0

Return -1

Else ;case insensative comparison

If $vA > $vB Then Return 1

If $vA = $vB Then Return 0

Return -1

EndIf

EndFunc

Thank you both for posting :)

edit: fixed typo in the function definition

Edited by blindwig

Share this post


Link to post
Share on other sites

You might want to look at this

If Not IsString($vB) Then $vA = String($vB)

???

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I normally refrain from posting after a situation similar to this......

I took the time and created, at the best of my ability for someone asking for help, a small script that would "fulfill" the need of the alphabetic (if not case-sensative) comparison posts... and I was slammed at a level that reflects a type of jealousy from the original poster... like this...

@Valuater:

I'm not sure how you think that has anything to do with what I asked.  Your code uses >= which I've already stated is not case sensative.  Besides that, you convert all the strings to uppercase to compare them, which would destory any routine for case sensativity, and finally you run the output through StringProper which totally destroys the original case of the strings.

Here's the routine I wrote - it can compare numbers or strings, and has a flag for comparing case sensative strings:

<{POST_SNAPBACK}>

I certainly believe my script can compare numbers, strings and alphabetic (if not case sensitive) comparaisons with the simple removal of the nicer looking "StringProper" out-put

The original $String is not destroyed until the (removeable) "StringProper"

Not only this... but July 15 2005, approx 10:03 +/- 2 hours 20 min ealier.. the poster placed his reasoning

(as asked by JSThePatriot)

For sorting a list of strings. Like _ArraySort does, but I wanted to add a case sense option to my routine.

I cannot understand how this could be true.... the posters similar to "array sort" (i would imagine) could have only 2, however would more likely have 3 to 10+ levels. My "help only" script handles 3 levels (that could be expanded) and out-puts the three original strings inputed as sorted alphabetacally (you could use numbers also). The poster's handles only two at a time and out-puts 1,0,-1.

How much of a routine (or other functions) does the posters script require to accomplish this....

$String_[1] = "dave"

$String_[2] = "MARCY"

$String_[3] = "Andy"

to create a similar acceptable out-put?

More than this... to criticise a "logical" reply to the posters "help" request with an actual function in less than 2 1/2 hours is simply... Rude

Sorry, I just had to say something this time.... If I am "way off base" I would love to hear it from others.... not the original poster

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0