?
Solved

How to swap member or transpose elements in a dynamic array

Posted on 2011-10-20
13
Medium Priority
?
325 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:chtullu135
  • 7
  • 4
  • 2
13 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37003334
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
 

Author Comment

by:chtullu135
ID: 37003554
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
 
LVL 44

Expert Comment

by:GRayL
ID: 37003563
Just to make sure we're on the same page:

1,2,3

becomes

2,3,?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 37003569
given "Z123456789",
what should be the final output ?
0
 
LVL 44

Expert Comment

by:GRayL
ID: 37003587
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
 
LVL 44

Expert Comment

by:GRayL
ID: 37003604
Or I suppose you could stick the value of the first element in a separate variable before starting the shifting.
0
 

Author Comment

by:chtullu135
ID: 37007910
Hello Capricorn,
Given the following string
"Z123456789"
The desired output would be
1Z32547698
0
 
LVL 44

Accepted Solution

by:
GRayL earned 2000 total points
ID: 37008270
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
 
LVL 44

Expert Comment

by:GRayL
ID: 37008291
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
 
LVL 44

Expert Comment

by:GRayL
ID: 37012660
Comments?
0
 

Author Comment

by:chtullu135
ID: 37012794
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
 

Author Closing Comment

by:chtullu135
ID: 37033287
Thanks again for your help
0
 
LVL 44

Expert Comment

by:GRayL
ID: 37033306
Thanks, glad to help.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question