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

Juan VelasquezAsked:
Who is Participating?
 
GRayLConnect With a Mentor Commented:
Try this:

Public Function Rev2AtATime(myStr As String) As String
Dim i As Integer, prefix As String, suffix As String, mid2 As String
For i = 1 To 9 Step 2
  prefix = Mid(myStr, 1, i - 1)
  mid2 = StrReverse(Mid(myStr, i, 2))
  suffix = Mid(myStr, i + 2)
  myStr = prefix & mid2 & suffix
Next i
Rev2AtATime = myStr
End Function
0
 
Rey Obrero (Capricorn1)Commented:
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

0
 
Juan VelasquezAuthor Commented:
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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

1,2,3

becomes

2,3,?
0
 
Rey Obrero (Capricorn1)Commented:
given "Z123456789",
what should be the final output ?
0
 
GRayLCommented:
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.
0
 
GRayLCommented:
Or I suppose you could stick the value of the first element in a separate variable before starting the shifting.
0
 
Juan VelasquezAuthor Commented:
Hello Capricorn,
Given the following string
"Z123456789"
The desired output would be
1Z32547698
0
 
GRayLCommented:
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.
0
 
GRayLCommented:
Comments?
0
 
Juan VelasquezAuthor Commented:
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.
0
 
Juan VelasquezAuthor Commented:
Thanks again for your help
0
 
GRayLCommented:
Thanks, glad to help.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.