Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Posts: 947 Joined: Wed Nov 09, 2011 8:58 am
Post
by firace » Sat May 16, 2015 9:36 am
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!
wilbert
PureBasic Expert
Posts: 3943 Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands
Post
by wilbert » Sat May 16, 2015 9:49 am
PB string positions start with 1, not 0 so your loop should be from 1 to Len(s1)
Windows (x64)
Raspberry Pi OS (Arm64)
firace
Addict
Posts: 947 Joined: Wed Nov 09, 2011 8:58 am
Post
by firace » Sat May 16, 2015 9:52 am
wilbert wrote: PB string positions start with 1, not 0 so your loop should be from 1 to Len(s1)
Ouch, of course!! Thanks!
Lord
Addict
Posts: 907 Joined: Tue May 26, 2009 2:11 pm
Post
by Lord » Sat May 16, 2015 10:00 am
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
STARGÅTE
Addict
Posts: 2260 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Sat May 16, 2015 11:23 am
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
Last edited by
STARGÅTE on Sat May 16, 2015 11:27 am, edited 1 time in total.
IdeasVacuum
Always Here
Posts: 6426 Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:
Post
by IdeasVacuum » Sat May 16, 2015 11:24 am
All these solutions rely on using ASCII - but what about Unicode strings?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
said
Enthusiast
Posts: 342 Joined: Thu Apr 14, 2011 6:07 pm
Post
by said » Sat May 16, 2015 11:42 am
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)
Lord
Addict
Posts: 907 Joined: Tue May 26, 2009 2:11 pm
Post
by Lord » Sat May 16, 2015 11:51 am
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.
STARGÅTE
Addict
Posts: 2260 Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:
Post
by STARGÅTE » Sat May 16, 2015 1:00 pm
Lord
Addict
Posts: 907 Joined: Tue May 26, 2009 2:11 pm
Post
by Lord » Sat May 16, 2015 1:15 pm
As I already stated: BAD NEWS!
Keep BASIC basic!
Tenaja
Addict
Posts: 1959 Joined: Tue Nov 09, 2010 10:15 pm
Post
by Tenaja » Sat May 16, 2015 3:11 pm
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.
Lord
Addict
Posts: 907 Joined: Tue May 26, 2009 2:11 pm
Post
by Lord » Sat May 16, 2015 4:52 pm
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.
marroh
User
Posts: 72 Joined: Wed Aug 06, 2008 8:21 am
Post
by marroh » Sun May 17, 2015 11:13 am
[offtopic on]
Lord wrote: And not everybody wants/needs unicode.
+1
[offtopic off]
Pure BASIC v5.41 LTS , Windows v8.1 x64
Forget UNICODE - Keep it BASIC !
Tenaja
Addict
Posts: 1959 Joined: Tue Nov 09, 2010 10:15 pm
Post
by Tenaja » Sun May 17, 2015 6:21 pm
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.
netmaestro
PureBasic Bullfrog
Posts: 8452 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Tue May 19, 2015 7:25 pm
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.
BERESHEIT