Thread: Scramble a string with conditions Thanks: 0 Likes: 0

1. Re: Scramble a string with conditions

And these two:
Code:
comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)
Could be replaced with:
Code:
comb = Replace(Left(comb, Len(comb) - 1), str & ",", "")  Reply With Quote

2. Re: Scramble a string with conditions

Alright.

Very comprehensive.

I am gradually updating my Intel   Reply With Quote

3. Re: Scramble a string with conditions Originally Posted by footoo And these two:
Code:
comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)
Could be replaced with:
Code:
comb = Replace(Left(comb, Len(comb) - 1), str & ",", "")
The code is return only one scrambled item after I updated the versions of code you suggested I replace.

Even after I returned to original it's still returning just one  Reply With Quote

4. Re: Scramble a string with conditions

The code just worked again mysteriously.

I was then using the i variable as Long. Could that be a reason?  Reply With Quote

5. Re: Scramble a string with conditions Originally Posted by footoo Code:
Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABCD"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = LBound(ray) To UBound(ray) - 1
comb = comb & ray(i) & ","
Next
comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)
End Sub

Function Get_Comb(comb As String, s1 As String, s2 As String)
Dim i%, sLen%
sLen = Len(s2)
If sLen < 2 Then
comb = comb & s1 & s2 & ","
Else
For i = 1 To sLen
Call Get_Comb(comb, s1 + Mid(s2, i, 1), Left(s2, i - 1) + Right(s2, sLen - i))
Next
End If
Get_Comb = comb
End Function

Function Remove_Dup(ray As Variant) As Variant
Dim i%, d As Object
Set d = CreateObject("Scripting.Dictionary")
For i = LBound(ray) To UBound(ray)
If IsMissing(ray(i)) = False Then d.Item(ray(i)) = 1
Next
Remove_Dup = d.Keys
End Function

So I tried to store the items in a vertical list by doing:

Code:
Code:
Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABCD"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = LBound(ray) To UBound(ray) - 1
comb = comb & " " & i  + 1 & " " & ray(i) & vbCr
Next
comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)
End Sub

Function Get_Comb(comb As String, s1 As String, s2 As String)
Dim i%, sLen%
sLen = Len(s2)
If sLen < 2 Then
comb = comb & s1 & s2 & ","
Else
For i = 1 To sLen
Call Get_Comb(comb, s1 + Mid(s2, i, 1), Left(s2, i - 1) + Right(s2, sLen - i))
Next
End If
Get_Comb = comb
End Function

Function Remove_Dup(ray As Variant) As Variant
Dim i%, d As Object
Set d = CreateObject("Scripting.Dictionary")
For i = LBound(ray) To UBound(ray)
If IsMissing(ray(i)) = False Then d.Item(ray(i)) = 1
Next
Remove_Dup = d.Keys
End Function
But the numbering is not looking good. Can someone help fix it for me?  Reply With Quote

6. Re: Scramble a string with conditions

Code:
Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABBA"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = 1 To UBound(ray) - 1
comb = comb & " " & i & " " & ray(i) & vbCrLf
Next
End Sub  Reply With Quote

7. Re: Scramble a string with conditions Originally Posted by footoo Code:
Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABBA"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = 1 To UBound(ray) - 1
comb = comb & " " & i & " " & ray(i) & vbCrLf
Next
End Sub
Wow!!!

This is unbelievable. Why did the vbcr give me some issues but not the vbcrlf ?  Reply With Quote

User Tag List

Tags for this Thread

dim, output, scramble, store, string  Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•