Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

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

  1. #11
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,571
    Post Thanks / Like
    Mentioned
    36 Post(s)
    Tagged
    1 Thread(s)

    Default 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 & ",", "")

  2. #12
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,505
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Scramble a string with conditions

    Alright.

    Very comprehensive.

    I am gradually updating my Intel
    There Is Always A Better Way

  3. #13
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,505
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Scramble a string with conditions

    Quote Originally Posted by footoo View Post
    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
    There Is Always A Better Way

  4. #14
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,505
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default 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?
    There Is Always A Better Way

  5. #15
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,505
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Scramble a string with conditions

    Quote Originally Posted by footoo View Post
    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?
    There Is Always A Better Way

  6. #16
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,571
    Post Thanks / Like
    Mentioned
    36 Post(s)
    Tagged
    1 Thread(s)

    Default 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

  7. #17
    Board Regular kelly mort's Avatar
    Join Date
    Apr 2017
    Location
    Suhum, Ghana, West Africa
    Posts
    1,505
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Scramble a string with conditions

    Quote Originally Posted by footoo View Post
    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 ?
    There Is Always A Better Way

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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