Page 1 of 1
Comparing strings char by char
Posted: Sat May 16, 2015 9:36 am
by firace
I want to compare 2 strings of equal length and find which characters differ and which are equal.
I managed to do this with the below code (note: must be compiled as ASCII), but one little detail that I don't understand is why I need to subtract 1 from @r0+i in the PokeC. Could anyone with a sharper brain answer this one?
Code: Select all
s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
For i = 0 To Len (s1)
If Mid(s1,i,1) = Mid(s2,i,1) : PokeC (@r0+i-1,'=') : EndIf
Next
Debug r0
Thanks!
Re: Comparing strings char by char
Posted: Sat May 16, 2015 9:49 am
by wilbert
PB string positions start with 1, not 0 so your loop should be from 1 to Len(s1)
Re: Comparing strings char by char
Posted: Sat May 16, 2015 9:52 am
by firace
wilbert wrote:PB string positions start with 1, not 0 so your loop should be from 1 to Len(s1)
Ouch, of course!! Thanks!

Re: Comparing strings char by char
Posted: Sat May 16, 2015 10:00 am
by Lord
Why not using PeekC()?
Code: Select all
s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
AdrS1.i=@s1
AdrS2.i=@s2
AdrR0.i=@r0
L.i=Len(s1)-1
For i = 0 To L
If PeekC(AdrS1+i) = PeekC(AdrS2+i) : PokeC (AdrR0+i,'=') : EndIf
Next
Debug r0
Re: Comparing strings char by char
Posted: Sat May 16, 2015 11:23 am
by STARGÅTE
This code doesn't work with unicode.
Here an other example:
Code: Select all
Structure CharacterArray
C.C[0]
EndStructure
s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
*AdrS1.CharacterArray = @s1
*AdrS2.CharacterArray = @s2
*AdrR0.CharacterArray = @r0
Define I = 0
While *AdrR0\C[I]
If *AdrS1\C[I] = *AdrS2\C[I]
*AdrR0\C[I] = '='
EndIf
I + 1
Wend
Debug r0
@IdeasVacuum: My code is unicode compatible
Re: Comparing strings char by char
Posted: Sat May 16, 2015 11:24 am
by IdeasVacuum
All these solutions rely on using ASCII - but what about Unicode strings?
Re: Comparing strings char by char
Posted: Sat May 16, 2015 11:42 am
by said
Hi,
An alternative that works with ascii/unicode (and faster than Poke/Peek)
Code: Select all
s1.s = "24cdef5e8db4afca39a4198d4df5d6e4"
s2.s = "14cd9f5e8db2afca39a41c8d4dfcd6eb"
r0.s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
*C1.Character = @s1
*C2.Character = @s2
*C3.Character = @r0
While *C1\c
If *C1\c = *C2\c : *C3\c = '=' : EndIf
*C1 + SizeOf(Character)
*C2 + SizeOf(Character)
*C3 + SizeOf(Character)
Wend
Debug r0
edit: updated to include the *Ci + .... (lost with copy/paste)
Re: Comparing strings char by char
Posted: Sat May 16, 2015 11:51 am
by Lord
STARGÅTE wrote:This code doesn't work with unicode.
...[/code]
The premise was:
firace wrote:...
(note: must be compiled as ASCII)
...
And not everybody wants/needs unicode.
Leave BASIC basic.
Re: Comparing strings char by char
Posted: Sat May 16, 2015 1:00 pm
by STARGÅTE
Re: Comparing strings char by char
Posted: Sat May 16, 2015 1:15 pm
by Lord
As I already stated: BAD NEWS!
Keep BASIC basic!
Re: Comparing strings char by char
Posted: Sat May 16, 2015 3:11 pm
by Tenaja
Lord wrote:Keep BASIC basic!
ASCII vs Unicode has nothing to do with BASIC being basic.
Please imagine what you are saying, from a the perspective of other people...
Lord wrote:Keep BASIC English.
Because English is one of the few languages without unicode characters.
Re: Comparing strings char by char
Posted: Sat May 16, 2015 4:52 pm
by Lord
Tenaja wrote:...
Lord wrote:Keep BASIC English.
Because English is one of the few languages without unicode characters.
So what?
My native language is not English.
Re: Comparing strings char by char
Posted: Sun May 17, 2015 11:13 am
by marroh
[offtopic on]
Lord wrote:And not everybody wants/needs unicode.
+1
[offtopic off]
Re: Comparing strings char by char
Posted: Sun May 17, 2015 6:21 pm
by Tenaja
marroh wrote:[offtopic on]
Lord wrote:And not everybody wants/needs unicode.
+1
[offtopic off]
Your complaints are moot, considering the decisions of the dev team. OTOH, you can always use a "legacy" version of PB.
Re: Comparing strings char by char
Posted: Tue May 19, 2015 7:25 pm
by netmaestro
Just one point to consider, depending upon how many string comparisons would test as equal you might realize a considerable speed gain by first doing a hash check on the strings and if they are the same, stop there. If not, then compare char by char.