Announcement

Collapse
No announcement yet.

swap string-arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • swap string-arrays

    I want to swap two string-arrays.
    Is that possible?
    Any help appreciated


    ------------------
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se
    Fred
    mailto:[email protected][email protected]</A>
    http://www.oxenby.se

  • #2
    Code:
    DIM array1(...) AS STRING
    DIM array2(...) AS STRING
       
    numSwap& = 100  ' let's swap 100 elements
       
    temp$ = PEEK$(VARPTR(array1(0)), numSwap& * 4)
    POKE$ VARPTR(array1(0)), PEEK$(VARPTR(array2(0)), numSwap& * 4)
    POKE$ VARPTR(array2(0)), temp$
    This *should* work

    Lothar

    ------------------

    Comment


    • #3
      Not to be overly simplistic, but what about the SWAP verb?

      Code:
      SWAP Array1(n), Array2(n)
      MCM
      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        look into QuickSortF_A (SC Forum) and find ExchangeDynamic.
        It shows how to swap 2 Strings by swapping the pointers to the string data (lightning fast). The this code works on with any dynamic string.

        Regards,

        Torsten




        ------------------

        Comment


        • #5
          I want to swap the whole string-array, not only a single string in the array.
          That is, Lothar came close to what I wanted.
          Code:
          Sub SwapArray(a$(),b$())
          Local upper&, _
                lower&, _
                arrayLen&, _
                tmp$
             
            lower& = LBound(a$)
            upper& = UBound(a$)
            ReDim b$(lower& To upper&)
            arrayLen& =((upper& - lower&)+1)* 4
            temp$ = Peek$(VarPtr(a$(lower&)),arrayLen&)
            Poke$ VarPtr(a$(lower&)),Peek$(VarPtr(b$(lower&)),arrayLen&)
            Poke$ VarPtr(b$(lower&)),temp$
          End Sub
            
          Function PbMain
          Local x()As String
          Local y()As String
            
            ReDim x(1 To 5)
            ReDim y(0)
            x(1) = "String 1"
            x(2) = "String 2"
            x(3) = "String 3"
            x(4) = "String 4"
            x(5) = "String 5"
            
            For i& = 1 To 5
              Print x(i&)
            Next
              
            SwapArray x(),y()
               
            For i& = LBound(y) To UBound(y)
              Print y(i&)
            Next
            
            waitkey$
          End Function
          Thank's alot, Lothar.This is code I understand


          ------------------
          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se



          [This message has been edited by Fred Oxenby (edited February 28, 2002).]
          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Comment


          • #6
            oops!
            copyArray might not be a very bright idea!
            code deleted...

            ------------------
            Fred
            mailto:[email protected][email protected]</A>
            http://www.oxenby.se

            [This message has been edited by Fred Oxenby (edited February 28, 2002).]
            Fred
            mailto:[email protected][email protected]</A>
            http://www.oxenby.se

            Comment


            • #7
              Do someone have a trustworthy/correct length of the array_descriptor?
              I do not really need to know what's stored in it, just the length..
              Thank you


              ------------------
              Fred
              mailto:[email protected][email protected]</A>
              http://www.oxenby.se

              [This message has been edited by Fred Oxenby (edited February 28, 2002).]
              Fred
              mailto:[email protected][email protected]</A>
              http://www.oxenby.se

              Comment


              • #8
                Fred,

                i bet nobody will bet this!!!!!!
                i is real, and it works.

                Code:
                #Compile Exe
                #Register None
                
                Sub SwapStringArray(ByVal aPtr1 As Dword, ByVal aPtr2 As Dword)
                
                    !MOV ECX, aPtr1
                    !MOV EDX, aPtr2
                
                    !MOV EAX, [ECX]
                    !MOV EBX, [EDX]
                    !MOV [ECX], EBX
                    !MOV [EDX], EAX
                    
                End Sub
                
                Function PbMain
                    
                    Dim s1(0 To 2) As String
                    Dim s2(0 To 2) As String
                
                    s1(0) = "s1_0"
                    s1(1) = "s1_1"
                    s1(2) = "s1_2"
                
                    s2(0) = "s2_0"
                    s2(1) = "s2_1"
                    s2(2) = "s2_2"
                
                    Call SwapStringArray(VarPtr(s1()), VarPtr(s2()))
                    
                    MsgBox s1(0) + $CrLf + s1(1) + $CrLf + s1(2) + $CrLf + s2(0) + $CrLf + s2(1) + $CrLf + s2(2)
                    
                End Function
                for the case that you do not trust an example with strings of different length's.

                Code:
                Function PbMain
                    
                    Dim s1(0 To 2) As String
                    Dim s2(0 To 2) As String
                
                    s1(0) = "s1_0"
                    s1(1) = "s1___1"
                    s1(2) = "s12"
                
                    s2(0) = "s2___0"
                    s2(1) = "s2_1"
                    s2(2) = "s2__2"
                
                    Call SwapStringArray(VarPtr(s1()), VarPtr(s2()))
                    
                    MsgBox s1(0) + $CrLf + s1(1) + $CrLf + s1(2) + $CrLf + s2(0) + $CrLf + s2(1) + $CrLf + s2(2)
                    
                End Function
                ------------------


                [This message has been edited by Torsten Rienow (edited February 28, 2002).]

                Comment


                • #9
                  Thank you guys,
                  It is not that I do not trust your examples...
                  It is more like I did not know what I wanted or needed...
                  But now, I know what functionallity I need.
                  Code:
                  1)I have two String-Array a$() and b$()
                  2)Then I want to be able to access this two string-arrays, using the name 
                    of a third string-array [c$()]
                  3)That is,I want to set the adress of c$()'s array-descriptor to either the address
                    of a$()'s array-descriptor or b$()'s array-descriptor.
                  4) changing a$() or b$() will be visible instantly in c$()
                  Dead easy, if you know how to do it.
                  Unfortunatly I don't


                  ------------------
                  Fred
                  mailto:[email protected][email protected]</A>
                  http://www.oxenby.se

                  [This message has been edited by Fred Oxenby (edited February 28, 2002).]
                  Fred
                  mailto:[email protected][email protected]</A>
                  http://www.oxenby.se

                  Comment


                  • #10
                    Rather than fooling with undocumented portions of the compiled code (which could be subject to change from version to version of PowerBASIC), why not just create a Sub that takes an array BYREF...?
                    Code:
                    DIM a(1:100) AS LONG
                    DIM b(-2000:-1000) AS LONG
                    CALL ProcessArray(a())
                    CALL ProcessArray(b())
                    ...
                    SUB ProcessArray(c() AS LONG)
                    ... use UBOUND,LBOUND etc to manipulate/analyse array content
                    END SUB
                    Seems to me that it achieves the same result - referencing an array by another name.

                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      Lance,
                      Well, If I could have solved my problem by calling a function like that,
                      I would not have give someting else much thought.
                      I am trying to find "an easy logic" to to fill a virtual listview with items
                      from diffrent string-arrays (real big ones).



                      ------------------
                      Fred
                      mailto:[email protected][email protected]</A>
                      http://www.oxenby.se
                      Fred
                      mailto:[email protected][email protected]</A>
                      http://www.oxenby.se

                      Comment


                      • #12
                        DIM newArray(size) AS type AT VARPTR(oldarray(0))

                        MCM
                        (Not tested for dynamic strings but I think it should work. VARPTR(dynamicstring)returns the address of the handle).

                        Michael Mattias
                        Tal Systems Inc. (retired)
                        Racine WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          Fred,

                          try this.

                          Regards,

                          Torsten

                          Code:
                          #Compile Exe
                          #Register None
                          
                          Sub DuplicateStringArrayRef(ByVal aPtrSrc As Dword, ByVal aPtrDst As Dword)
                              
                              !MOV ECX, aPtrSrc
                              !MOV EDX, aPtrDst
                          
                              !MOV EAX, [ECX]
                              !MOV [EDX], EAX
                              
                          End Sub
                          
                          Sub SwapStringArray(ByVal aPtr1 As Dword, ByVal aPtr2 As Dword)
                          
                              !MOV ECX, aPtr1
                              !MOV EDX, aPtr2
                          
                              !MOV EAX, [ECX]
                              !MOV EBX, [EDX]
                              !MOV [ECX], EBX
                              !MOV [EDX], EAX
                              
                          End Sub
                          
                          Function PbMain
                              
                          
                              Dim s1(0 To 2) As String
                              Dim s2(0 To 2) As String
                          
                          '   This is a workaround for a long known PB Compiler Bug/Feature. PB Compiler will know and be
                          '   sure at compiletime, that an array is dimensioned. Pb does not respect the fact, that
                          '   an array just declared, but not dimensioned could be given byref to a procedure that will
                          '   dimension the array. At the other way i am very thankfull for that feature, it opened my
                          '   mind very wide for direct memory access under window$.
                          '   This Bug/Feature was reported a year ago or longer. If you want to see what i mean,
                          '   comment the following two lines and uncomment the line with ''''.
                          
                              Dim s3(0) As String
                              Erase s3()
                          
                          ''''Dim s3() as String
                          
                              s1(0) = "s1_0"
                              s1(1) = "s1___1"
                              s1(2) = "s12"
                          
                              s2(0) = "s2___0"
                              s2(1) = "s2_1"
                              s2(2) = "s2__2"
                          
                          '    Call SwapStringArray(Varptr(s1()), Varptr(s2()))
                          
                          '   s1 handle will be duplicated to s3, any change in any array will affect both arrays.
                              Call DuplicateStringArrayRef(VarPtr(s1()), VarPtr(s3()))
                              s1(1) = "Hallo"
                              s3(2) = "Fred"
                              MsgBox s1(0) + $CrLf + s1(1) + $CrLf + s1(2) + $CrLf + s3(0) + $CrLf + s3(1) + $CrLf + s3(2)
                          
                              
                          End Function
                          ------------------




                          [This message has been edited by Torsten Rienow (edited March 01, 2002).]

                          Comment


                          • #14
                            Hi Fred,

                            How about this:

                            Regards
                            Peter
                            Code:
                            #compile exe
                            #dim all
                            #register none
                             
                            function pbmain
                             
                                dim a(2) as string
                                dim b(4) as string
                                dim c as string pointer
                             
                                a(0) = "a0"
                                a(1) = "a1"
                                a(2) = "a2"
                             
                                b(0) = "b0"
                                b(1) = "b1"
                                b(2) = "b2"
                                b(3) = "b3"
                                b(4) = "b4"
                             
                                ' c = a
                                c = varptr(a(0))
                                msgbox @c[1]
                             
                                ' c = b
                                c = varptr(b(0))
                                msgbox @c[3]
                             
                                ' Change b
                                b(3) = "TEST"
                                msgbox @c[3]
                             
                            end function


                            ------------------
                            [email protected]
                            [email protected]
                            www.dreammodel.dk

                            Comment


                            • #15
                              Originally posted by Fred Oxenby:
                              Thank you guys,
                              Code:
                              1)I have two String-Array a$() and b$()
                              2)Then I want to be able to access this two string-arrays, using the name 
                                of a third string-array [c$()]
                              3)That is,I want to set the adress of c$()'s array-descriptor to either the address
                                of a$()'s array-descriptor or b$()'s array-descriptor.
                              4) changing a$() or b$() will be visible instantly in c$()

                              If I'm reading this post right, you might consider:

                              if x = y then
                              call CopyA(a$(),c$(),i)
                              else
                              call CopyB(b$(),c$(),i)
                              end if

                              rem "i" points to the last string in the array

                              sub CopyA(a$(),c$(),i)
                              for x = 1 to i
                              c$(x) = a$(x)
                              next x
                              end sub

                              sub CopyB(b$(),c$(),i)
                              for x = 1 to i
                              c$(x) = b$(x)
                              next x
                              end sub




                              ------------------
                              There are no atheists in a fox hole or the morning of a math test.
                              If my flag offends you, I'll help you pack.

                              Comment


                              • #16
                                Peter, your suggestion for using indexed string pointers is simply excellent... in fact, I'll ask the Documentation Dept to add notes on that technique to the next edition of the help file.

                                Thanks!


                                ------------------
                                Lance
                                PowerBASIC Support
                                mailto:[email protected][email protected]</A>
                                Lance
                                mailto:[email protected]

                                Comment


                                • #17
                                  Torsten Rienow,Peter P Stephensen:
                                  Pure genius.
                                  I had read and read the part of indexed pointers but not understood.
                                  I have burned my a*s of trying to understand the Array-descriptor.
                                  I came up with following Udt.
                                  Code:
                                  Type PB_ArrayDescriptor
                                   AddressOfLowestElement   as string ptr (as Type Ptr etc)
                                   UnKnown                  As Long       '5-8
                                   NumberOfElement          As Long       '9-12
                                   TotalSizeOfArray         As Long       '13-16
                                   UnKnown2                 As Long       '17-20 maybe TotSize is quad..
                                   SizeOfSingleElement      As Long       '21-24
                                   LowerBound               As Long       '25-28
                                   UpperBound               As Long       '29-32
                                  End Type
                                  But your solutions are outstanding.
                                  I have to test and verify them, but they seem to be all that my simple mind tried to produce
                                  but failed to do.
                                  Thank you

                                  UPDATEDE:
                                  The struct was wrong, I forgot NumberOfElement as long


                                  ------------------
                                  Fred
                                  mailto:[email protected][email protected]</A>
                                  http://www.oxenby.se

                                  [This message has been edited by Fred Oxenby (edited March 03, 2002).]
                                  Fred
                                  mailto:[email protected][email protected]</A>
                                  http://www.oxenby.se

                                  Comment


                                  • #18
                                    Fred, Lance

                                    Thanks

                                    Regards
                                    Peter

                                    ------------------
                                    [email protected]
                                    [email protected]
                                    www.dreammodel.dk

                                    Comment


                                    • #19
                                      Fred,

                                      Actually your method could work:

                                      Regards
                                      Peter
                                      Code:
                                      #compile exe
                                      #dim all
                                      #register none
                                       
                                      function pbmain
                                       
                                          dim a(2) as string
                                          dim b(2) as string
                                          dim c(2) as string
                                          local pc as dword pointer : pc = varptr(c())
                                       
                                          a(0) = "a0"
                                          a(1) = "a1"
                                          a(2) = "a2"
                                       
                                          b(0) = "b0"
                                          b(1) = "b1"
                                          b(2) = "b2"
                                           
                                          @pc = varptr(b(0))
                                       
                                          msgbox c(2)
                                       
                                          b(2) = "TEST"
                                       
                                          msgbox c(2)
                                       
                                          @pc = varptr(a(0))
                                       
                                          msgbox c(2)
                                        
                                      end function
                                      ------------------
                                      [email protected]
                                      [email protected]
                                      www.dreammodel.dk

                                      Comment


                                      • #20
                                        The only method I have found to work, is following
                                        Code:
                                          Poke$ VarPtr(c()),Peek$(VarPtr(a()),32)
                                          Poke$ VarPtr(c()),Peek$(VarPtr(b()),32)
                                        This method makes it possible to query the current bound of the array c()
                                        But still, I dont know if the struct is 32 bytes or greater.
                                        But it has a great disadvantage, It will only mirror what was in the array
                                        at "swap-time"
                                        ------
                                        Torsten, I am not a asm-guru, but I think you only swap the address of
                                        the lowest element. That is, you indeed may access the strings by array-index
                                        but the "current" description of c() is not correct.


                                        ------------------
                                        Fred
                                        mailto:[email protected][email protected]</A>
                                        http://www.oxenby.se

                                        [This message has been edited by Fred Oxenby (edited March 03, 2002).]
                                        Fred
                                        mailto:[email protected][email protected]</A>
                                        http://www.oxenby.se

                                        Comment

                                        Working...
                                        X