Link to home
Start Free TrialLog in
Avatar of Juan Velasquez
Juan VelasquezFlag for United States of America

asked on

How to swap member or transpose elements in a dynamic array

Hello,
I am trying to figure out how to swap array elements in a dynamic array in a predefined way such that each element of the array is swapped with the next element.  I've gotten as for as creating the initial array via a split function .  I think I have to create a second array of the same size as the first array.  The first step is of course creating an array of the same size as the initial array.  I'm not sure how to do this?
Public Function NewSwap(strMemberID As String) As String

Dim i As Integer
Dim x As Integer
Dim strOutput As String
Dim arrOutput As Variant

For i = 1 To Len(strMemberID)
strOutput = strOutput & Mid$(strMemberID, i, 1) & IIf(i <> Len(strMemberID), ",", "")
Next

arrOutput = Split(strOutput, ",")
For x = LBound(arrOutput) To UBound(arrOutput)
  Debug.Print arrOutput(x)
Next
End Function

Open in new window

Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

try this

Public Function NewSwap(strMemberID As String) As String

Dim i As Integer
Dim x As Integer, j as integer
Dim strOutput As String
Dim arrOutput As Variant

dim outArr()

For i = 1 To Len(strMemberID)
strOutput = strOutput & Mid$(strMemberID, i, 1) & IIf(i <> Len(strMemberID), ",", "")
Next

arrOutput = Split(strOutput, ",")
For x = 1 To UBound(arrOutput)
   
  'populate the outArr
   redim preserve outArr(j)
   outArr(j)=arrOutput(x)
   j=j+1

   
Next
   ' add the first  arrOutput(0) to the end of outArr
   redim preserve outArr(j)
   outArr(j)=arrOutput(0)
  
End Function

Open in new window

Avatar of Juan Velasquez

ASKER

Hello Capricorn,

I've come up with the following function.  It's almost working.  However, when I pass
the following string "Z123456789", I get "912345678Z".  I want to swap all the other charaters so that the 1 is swapped with the two, the 3 is swapped with the four, the five is swapped with the 6 ...

Sincerely,

Juan Velasquez
Public Function NewSwap(strMemberID As String) As String

Dim i As Integer
Dim x As Integer
Dim strOutput As String
Dim arrOutput() As String
Dim arrSwapOutput() As String

For i = 1 To Len(strMemberID)
strOutput = strOutput & Mid$(strMemberID, i, 1) & IIf(i <> Len(strMemberID), ",", "")
Next

arrOutput = Split(strOutput, ",")

ReDim arrSwapOutput(UBound(arrOutput) + 1)

For x = LBound(arrOutput) To UBound(arrOutput)
'Debug.Print x
    
    arrSwapOutput(x) = arrOutput(x)
   
    'Debug.Print arrSwapOutput(x)

Next

For x = LBound(arrOutput) To UBound(arrOutput)
    If x = LBound(arrOutput) Then
        arrSwapOutput(x) = arrOutput(UBound(arrOutput))
    ElseIf x = UBound(arrOutput) Then
        arrSwapOutput(x) = arrOutput(LBound(arrOutput))
    Else
        arrSwapOutput(x) = arrOutput(x + 1)
    End If
Next x


For x = LBound(arrSwapOutput) To UBound(arrSwapOutput)
    Debug.Print arrSwapOutput(x)
Next


End Function

Open in new window

Just to make sure we're on the same page:

1,2,3

becomes

2,3,?
given "Z123456789",
what should be the final output ?
ie.  When you are at the last element, does it switch with the first - which was 1 but is now 2 -  if you want the third element to become 1, then you need a fourth element which stores the value of the first element before the switching starts so you can populate the last element.
Or I suppose you could stick the value of the first element in a separate variable before starting the shifting.
Hello Capricorn,
Given the following string
"Z123456789"
The desired output would be
1Z32547698
ASKER CERTIFIED SOLUTION
Avatar of GRayL
GRayL
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This assumes all your strings are 10 Characters long.  If that is not the case, advise, it is simlple to work with varying strings - remember, in all cases the string length has to be an even number.
Comments?
Sorry,

I just got back.  I'll try the solution you posted.  I've come up with another solution but will test out both solutions on Monday to see which is greater.  I also need to double check the data to verify that even strings will be passed.  I'll post my results on Monday.
Thanks again for your help
Thanks, glad to help.