Compare Strings stored in to different allocated memory

Just starting out? Need help? Post your questions and find answers here.
martin66119
User
User
Posts: 46
Joined: Sat Jan 08, 2005 7:46 pm

Compare Strings stored in to different allocated memory

Post by martin66119 »

Goog evening!

Sorry for question which is quite similar to my other.

I stored in 2 different allocated memory sections some strings. Now I would like to find which string in BufferSting2 is also in BufferString1.

I have no idea to do this. Please could you help me!

Thank you very much in advance.

Code: Select all

Dim a.s(10)
Dim b.s(10)
a(1) = "Wald"
a(2) = "Baum"
a(3) = "Strauch"
a(4) = "Blatt"

b(1) = "Blatt"
b(2) = "Stein"
b(3) = "Wasser"
b(4) = "Wald"
b(5) = "Obst"

  String1$ = a(1)
  *String1 = @String1$   
  
  String2$ = b(1)
  *String2 =@String2$
  
  *BufferString1 = AllocateMemory(1000000)
  *PointerString1 = *BufferString1
  
  *BufferString2 = AllocateMemory(1000000)
  *PointerString2 = *BufferString2 
;-----------------------------------------------------------  
  CopyMemoryString(a(1), @*PointerString1)
  
  For i= 2 To 4
    CopyMemoryString(a(i))
  Next
;----------------------------------------------------------- 
  CopyMemoryString(b(1), @*PointerString2)

 For i = 2 To 5
  CopyMemoryString(b(i))
 Next
  ;---------------------------------------------------------- 
  Debug PeekS(*BufferString1)
  Debug PeekS(*BufferString2)



buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Compare Strings stored in to different allocated memory

Post by buddymatkona »

One way is to compare new string arrays using RegEx.

Code: Select all

Debug " " : Debug "Strings from BufferString2 also found in BufferString1: " 
  If CreateRegularExpression(0, "[A-Z][a-z]*") ; This RegEx assumes all strings are proper names. If not, you will have to define EXACTLY what you are looking for.
    Dim Buf1Strs.s(0) : Dim Buf2Strs.s(0)
    Found1 = ExtractRegularExpression(0, PeekS(*BufferString1), Buf1Strs())
    Found2 = ExtractRegularExpression(0, PeekS(*BufferString2), Buf2Strs())
    For k2 = 0 To Found2-1         
      For k1 = 0 To Found1-1
        If Buf2Strs(k2) = Buf1Strs(k1)
          Debug Buf2Strs(k2)
          Break
        EndIf
      Next
    Next 
  Else
    Debug RegularExpressionError()
  EndIf
martin66119
User
User
Posts: 46
Joined: Sat Jan 08, 2005 7:46 pm

Re: Compare Strings stored in to different allocated memory

Post by martin66119 »

Thank you very much for your help buddymatkona!

And sorry for asking almost the same question again.

Now I have a testcode to write string in an allocated memory and some other strings an an other allocated memory. In order provide the string length of individual string I use an auxillary array a_laenge () for the strings in array a() y and an other auxillar array b_laenge() for the strings in array b(). In the loop between "ElapsedMilliseconds() " I try to find out if a string in allocatedmemory b is also in allocatedmemory a or not. But I do not know to do this because I have no idea to use the function "comparememory and set (calculate) the correct length in the compare function.

Please could someone help me with a code!

Right now I use the following code.

Code: Select all

Dim a.s(20000)
Dim b.s(20000)
Dim c.s(20000)
Dim a_laenge(20000)
Dim b_laenge(20000)

a(1) = "Wald"
a(2) = "Baum"
a(3) = "Ast"
a(4) = "Blatt"

b(1) = "Hund"
b(2) = "Maus"
b(3) = "Katze"
b(4) = "Ast"
b(5) = "Vogel"
b(6) = "Wald"


Anzahl_a = 4              
Anzahl_b = 6            
            
  String1$ = a(1)
  *String1 = @String1$   
  
  String2$ = b(1)
  *String2 =@String2$
  
  *BufferString1 = AllocateMemory(10000000)
  *PointerString1 = *BufferString1
  
  *BufferString2 = AllocateMemory(10000000)
  *PointerString2 = *BufferString2 
;-----------------------------------------------------------  

Ergebnis = CopyMemoryString(a(1), @*PointerString1)
a_laenge(1) = Len(a(1))
 
For i= 2 To Anzahl_a
  CopyMemoryString(a(i)) 
  a_laenge(i) = Len(a(i))
  ;Debug PeekS(*BufferString1)
Next
 
;----------------------------------------------------------- 
  CopyMemoryString(b(1), @*PointerString2)
  b_laenge(1) = Len(b(1))
 For i = 2 To Anzahl_b
   CopyMemoryString(b(i))
   b_laenge(i) = Len(b(i))
 Next
  ;---------------------------------------------------------- 
  ;Debug PeekS(*BufferString1)
  ;Debug PeekS(*BufferString2)
  ;StringBuffer1$ = PeekS(*BufferString1)
  ;Debug StringBuffer1$ 
   *AdresseString2 = *BufferString2
  StartTime = ElapsedMilliseconds() 
  For i = 1 To Anzahl_b
    ;Debug b(i)
    ; How do I have to set and calculate *BufferString2 in order to use the function CompareMemory ()????????
    MessageRequester("",PeekS(*BufferString1, a_laenge(1)) + "  " + PeekS(*AdresseString2, b_laenge(i)))
     If CompareMemory(*BufferString1, *AdresseString2, b_laenge(i)) = 0 ;???????????????????
       NichtGefunden = NichtGefunden + 1     
       c(NichtGefunden) = PeekS(*BufferString2)       
     Else

     EndIf
   *AdresseString2 = *AdresseString2 + b_laenge(i)
   Next
   Time =ElapsedMilliseconds() - StartTime
   MessageRequester("Auswertezeit", Str(Time))
   MessageRequester("","Nicht gefunden" +  Str(NichtGefunden))
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Compare Strings stored in to different allocated memory

Post by wilbert »

Have you considered using a Map ?
You can put the strings from one block of memory in a Map and while going through the second list of strings, you can use FindMapElement() to check if it also was in the first list.
Windows (x64)
Raspberry Pi OS (Arm64)
martin66119
User
User
Posts: 46
Joined: Sat Jan 08, 2005 7:46 pm

Re: Compare Strings stored in to different allocated memory

Post by martin66119 »

Hi wilbert,

thank´s a lot for your help. I have tried to use map. It works well but it seems that comparememory is faster then using maps. Therefore I try to change the code.

Martin
Post Reply