Results 1 to 18 of 18

Thread: [RESOLVED] [ASK]Read and Separate

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Resolved [RESOLVED] [ASK]Read and Separate

    hi vbforumers.....
    now i have problem

    i have list of number on teksbox like this:
    45.65.667.54.222.4444.9876.20.999.8765.7654.10.90.21
    and my question is, how to separate the numbers that I have a set of numbers based on numbers like this??



  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Re: [ASK]Read and Separate

    may I ask for examples source code??

    thanks b4

  4. #4
    Lively Member
    Join Date
    Mar 2012
    Posts
    113

    Re: [ASK]Read and Separate

    Hope this helps

    Code:
    Dim longNumber As String
    Dim twoDigits, threeDigits, fourDigits As String
    Dim cnt As Integer
    longNumber = "45.65.667.54.222.4444.9876.20.999.8765.7654.10.90.21"
    
    'first seperate the numbers
    Dim splitLongNo() As String
    splitLongNo = Split(longNumber, ".")
    
    cnt = 1
       
    Do Until cnt >= UBound(splitLongNo)
        Select Case Len(splitLongNo(cnt))
            Case 2
                If twoDigits <> "" Then twoDigits = twoDigits & "." & splitLongNo(cnt) Else twoDigits = splitLongNo(cnt)
            Case 3
                If threeDigits <> "" Then threeDigits = threeDigits & "." & splitLongNo(cnt) Else threeDigits = splitLongNo(cnt)
            Case 4
                If fourDigits <> "" Then fourDigits = fourDigits & "." & splitLongNo(cnt) Else fourDigits = splitLongNo(cnt)
        End Select
    cnt = cnt + 1
    Loop
    'now  split the numbers from each
        Dim twoDigitArray() As String
        twoDigitArray = Split(twoDigits, ".")
        
        Dim threeDigitArray() As String
        threeDigitArray = Split(threeDigits, ".")
        
        Dim fourDigitArrays() As String
        fourDigitArrays = Split(fourDigits, ".")
        
        
    'now read the each block
    'read 2 digit numbers
     For x = 0 To UBound(twoDigitArray)
        MsgBox "2 Digit Numbers : " & twoDigitArray(x)
     Next
        
     'read 3 digit numbers
     For y = 0 To UBound(threeDigitArray)
        MsgBox "3 Digit Numbers : " & threeDigitArray(y)
     Next
     
     'read 4 digit numbers
     For z = 0 To UBound(fourDigitArrays)
        MsgBox "4 Digit Numbers : " & fourDigitArrays(z)
     Next

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Talking Re: [ASK]Read and Separate

    Quote Originally Posted by sam4help View Post
    Hope this helps

    Code:
    Dim longNumber As String
    Dim twoDigits, threeDigits, fourDigits As String
    Dim cnt As Integer
    longNumber = "45.65.667.54.222.4444.9876.20.999.8765.7654.10.90.21"
    
    'first seperate the numbers
    Dim splitLongNo() As String
    splitLongNo = Split(longNumber, ".")
    
    cnt = 1
       
    Do Until cnt >= UBound(splitLongNo)
        Select Case Len(splitLongNo(cnt))
            Case 2
                If twoDigits <> "" Then twoDigits = twoDigits & "." & splitLongNo(cnt) Else twoDigits = splitLongNo(cnt)
            Case 3
                If threeDigits <> "" Then threeDigits = threeDigits & "." & splitLongNo(cnt) Else threeDigits = splitLongNo(cnt)
            Case 4
                If fourDigits <> "" Then fourDigits = fourDigits & "." & splitLongNo(cnt) Else fourDigits = splitLongNo(cnt)
        End Select
    cnt = cnt + 1
    Loop
    'now  split the numbers from each
        Dim twoDigitArray() As String
        twoDigitArray = Split(twoDigits, ".")
        
        Dim threeDigitArray() As String
        threeDigitArray = Split(threeDigits, ".")
        
        Dim fourDigitArrays() As String
        fourDigitArrays = Split(fourDigits, ".")
        
        
    'now read the each block
    'read 2 digit numbers
     For x = 0 To UBound(twoDigitArray)
        MsgBox "2 Digit Numbers : " & twoDigitArray(x)
     Next
        
     'read 3 digit numbers
     For y = 0 To UBound(threeDigitArray)
        MsgBox "3 Digit Numbers : " & threeDigitArray(y)
     Next
     
     'read 4 digit numbers
     For z = 0 To UBound(fourDigitArrays)
        MsgBox "4 Digit Numbers : " & fourDigitArrays(z)
     Next
    wow....
    very......
    very....... helpful

    and I have 1 questions
    how to sort(asc/desc) the output (2 digit,3 digit and 4 digit)??


    once again thank you very much

  6. #6
    Lively Member
    Join Date
    Mar 2012
    Posts
    113

    Re: [ASK]Read and Separate

    you are welcome, good know it helped you;

    about sorting came across this code thought might help you;

    Code:
    Private Sub QuickSort(C() As String, ByVal First As Long, ByVal Last As Long)
        Dim Low As Long, High As Long
        Dim MidValue As String
        
        Low = First
        High = Last
        MidValue = C((First + Last) \ 2)
        
        Do
        While C(Low) < MidValue
        Low = Low + 1
        Wend
        
        While C(High) > MidValue
        High = High - 1
        Wend
        
        If Low <= High Then
            Swap C(Low), C(High)
            Low = Low + 1
            High = High - 1
        End If
    Loop While Low <= High
    
    If First < High Then QuickSort C, First, High
    If Low < Last Then QuickSort C, Low, Last
    End Sub
    
    Private Sub Swap(ByRef A As String, ByRef B As String)
        Dim T As String
        
        T = A
        A = B
        B = T
    End Sub
    and you need to call this for each digit arrays one by one like this

    Code:
    Call QuickSort(splitArrayName(), 0, UBound(splitArrayName))
    something like this in our case

    Code:
    'now read the each block
    'read 2 digit numbers
    Call QuickSort(twoDigitArray(), 0, UBound(twoDigitArray))
     For x = 0 To UBound(twoDigitArray)
        Debug.Print "2 Digit Numbers : " & twoDigitArray(x)
     Next
        
     'read 3 digit numbers
     Call QuickSort(threeDigitArray(), 0, UBound(threeDigitArray))
     For y = 0 To UBound(threeDigitArray)
        Debug.Print "3 Digit Numbers : " & threeDigitArray(y)
     Next
     
     'read 4 digit numbers
     Call QuickSort(fourDigitArrays(), 0, UBound(fourDigitArrays))
     For z = 0 To UBound(fourDigitArrays)
        Debug.Print "4 Digit Numbers : " & fourDigitArrays(z)
     Next
    Last edited by sam4help; Mar 13th, 2012 at 09:08 AM.

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Re: [ASK]Read and Separate

    omg.....
    many thanks master sam4help

    rep+ for you and RhinoBull

  8. #8
    Lively Member
    Join Date
    Mar 2012
    Posts
    113

    Re: [ASK]Read and Separate

    you are welcome, happy to know it helped you.

    All the very best,

    Sam

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Question Re: [ASK]Read and Separate

    thanks again vbforumers (sam4help, RhinoBull and other)

    now, i have new case. and the case is get value or digit after cross symbol
    Sample Data:
    45.65.667.54.222.4444.9876.20.999.8765.7654.10.21x72
    I have used the way from RhinoBull and sam4help , but never succeeded....

    can help me??

    and sample output:


    this list my case:
    - read the teks to get number then split number by digit >> Solved
    - and the last, Get number/digit after cross symbol >>>new and last case

  10. #10
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: [ASK]Read and Separate

    Afdoal

    Perhaps something like these changes I made
    to Sam's code ...

    Code:
    Dim longText As String
    Dim numAfterX as String
    Dim longNumber As String
    Dim twoDigits, threeDigits, fourDigits As String
    Dim cnt As Integer
    '
    longText = "45.65.667.54.222.4444.9876.20.999.8765.7654.10.90.21x72"
    v1 = Instr(longText, "x")
    numAfterX = Mid(longText, v1 + 1)      ' returns "72"
    longNumber = Left(longText, v1 - 1)    ' returns "45.65.667.54.222.4444.9876.20.999.8765.7654.10.90.21"
    'first seperate the numbers
    Dim splitLongNo() As String
    splitLongNo = Split(longNumber, ".")
    ... same as before
    The concept is to extract the "x72" first.
    Then, you can proceed per Sam's original code.

    Spoo

  11. #11
    Lively Member
    Join Date
    Mar 2012
    Posts
    113

    Re: [ASK]Read and Separate

    Try something like this,

    Code:
    Dim longNumber1, longNumber2 As String
    Dim twoDigits, threeDigits, fourDigits As String
    Dim cnt As Integer
    longNumber1 = "45.65.667.54.222.4444.9876.20.999.8765.7654.10.21x72"
    longNumber2 = Mid(longNumber1, 1, InStr(1, longNumber1, "x") - 1)
    
    'first seperate the numbers
    Dim splitLongNo() As String
    Dim splitLongNoAfterX() As String
    
    
    splitLongNo = Split(longNumber2, ".")
    splitLongNoAfterX = Split(longNumber1, "x")
    
    cnt = 1
       
    Do Until cnt >= UBound(splitLongNo)
        Select Case Len(splitLongNo(cnt))
            Case 2
                If twoDigits <> "" Then twoDigits = twoDigits & "." & splitLongNo(cnt) Else twoDigits = splitLongNo(cnt)
            Case 3
                If threeDigits <> "" Then threeDigits = threeDigits & "." & splitLongNo(cnt) Else threeDigits = splitLongNo(cnt)
            Case 4
                If fourDigits <> "" Then fourDigits = fourDigits & "." & splitLongNo(cnt) Else fourDigits = splitLongNo(cnt)
        End Select
    cnt = cnt + 1
    Loop
    'now  split the numbers from each
        Dim twoDigitArray() As String
        twoDigitArray = Split(twoDigits, ".")
        
        Dim threeDigitArray() As String
        threeDigitArray = Split(threeDigits, ".")
        
        Dim fourDigitArrays() As String
        fourDigitArrays = Split(fourDigits, ".")
        
        
    'now read the each block
    'read 2 digit numbers
     For x = 0 To UBound(twoDigitArray)
        Debug.Print "2 Digit Numbers : " & twoDigitArray(x)
     Next
        
     'read 3 digit numbers
     For y = 0 To UBound(threeDigitArray)
        Debug.Print "3 Digit Numbers : " & threeDigitArray(y)
     Next
     
     'read 4 digit numbers
     For z = 0 To UBound(fourDigitArrays)
        Debug.Print "4 Digit Numbers : " & fourDigitArrays(z)
     Next
     
     'read number after x
        Debug.Print "Number After X is : " & splitLongNoAfterX(1)

  12. #12

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Wink Re: [ASK]Read and Separate

    thanks Spoo and many thanks sam
    but, now my case have a change like this...


    can u help me for solved this??

    thanks b4

  13. #13
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: [ASK]Read and Separate

    Afdoal

    Well, what do you think the solution might be?

    I would say that you have the basics, especially from Sam ..
    Code:
    splitLongNo = Split(longNumber2, ".")
    splitLongNoAfterX = Split(longNumber1, "x")
    Instead of telling you what to do (you won't learn anything
    that way), it's time for you to put your thinking cap on ..
    • What is different with the text string you now are showing?
    • How might you use Sam's code to deal with it.

    Spoo

  14. #14

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Thumbs up Re: [ASK]Read and Separate

    thanks Spoo for your respon's

    before getting this case
    Code:
    958.30.65.667.54.222.4444.9876x10.20.999.8765.7654.10.90.21x125
    , I have a case like this:
    Code:
    958.30.65.667.54.222.4444.9876x10,20.999.8765.7654.10.90.21x125
    where the number of Digit After x (before the new dot(".")) is
    min=1
    max=4

    and i used this code for fro get the "X" location
    Code:
    'Spilit the text
    Private Function SplitTextNew(ByVal text As String, ByVal digit As Integer, Optional ByVal isDigitAfterX As Boolean = False) As String
        Dim arrText1()  As String
        Dim arrText2()  As String
    
        Dim tmpText     As String
        Dim i           As Integer
    
        arrText1 = Split(text, "x", , vbTextCompare)
        If isDigitAfterX Then
            SplitTextNew = arrText1(UBound(arrText1))
        Else
            arrText2 = Split(arrText1(0), ".")
            For i = 0 To UBound(arrText2)
                If Len(arrText2(i)) = digit Then
                    tmpText = tmpText & arrText2(i) & "."
                End If
            Next i
            If Len(tmpText) > 0 Then tmpText = Left(tmpText, Len(tmpText) - 1)
    
            SplitTextNew = tmpText
        End If
    End Function
    
    Private Sub Command7_Click()
        Dim strText As String
        Dim arrKoma() As String
        Dim arrX() As String
        Dim i As Integer
        strText = Text1.text
        If InStr(strText, ",") Then
            arrKoma = Split(strText, ",")
                For i = 0 To UBound(arrKoma)
                    arrX = Split(arrKoma(i), "x")
                    'save 2 digit
                    Set lstV = ListView1.ListItems.Add(, , SplitTextNew(arrX(0), 2))
                        lstV.SubItems(1) = SplitTextNew(arrX(1), 2, True)
                    
                    'save 3 digit
                    Set lstV = ListView2.ListItems.Add(, , SplitTextNew(arrX(0), 3))
                        lstV.SubItems(1) = SplitTextNew(arrX(1), 3, True)
    
                    'save 4 digit
                    Set lstV = ListView3.ListItems.Add(, , SplitTextNew(arrX(0), 4))
                        lstV.SubItems(1) = SplitTextNew(arrX(1), 4, True)
                    DoEvents
                Next i
        End If
    End Sub
    and now, on the new case, the coma(",") symbol have been change with dot (".") symbol
    Code:
    958.30.65.667.54.222.4444.9876x10.20.999.8765.7654.10.90.21x125
    iam confused, how to get the "." after Digit After "x" (in the previous case get the "," after Digit After "x")

  15. #15
    Lively Member
    Join Date
    Mar 2012
    Posts
    113

    Re: [ASK]Read and Separate

    try something like this

    Code:
    longNumber1 = "958.30.65.667.54.222.4444.9876x10.20.999.8765.7654.10.90.21x125"
    
    splitLongNoAfterX = Split(longNumber1, "x")
    
     
    'read number after x after dot
     For d = 0 To UBound(splitLongNoAfterX)
        Debug.Print "number after x till dot : " & IIf(InStr(splitLongNoAfterX(d), ".") = 0, splitLongNoAfterX(d), Replace(Mid(splitLongNoAfterX(d), 1, InStr(splitLongNoAfterX(d), ".")), ".", ""))
    Next

  16. #16

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Unhappy Re: [ASK]Read and Separate

    Quote Originally Posted by sam4help View Post
    try something like this

    Code:
    longNumber1 = "958.30.65.667.54.222.4444.9876x10.20.999.8765.7654.10.90.21x125"
    
    splitLongNoAfterX = Split(longNumber1, "x")
    
     
    'read number after x after dot
     For d = 0 To UBound(splitLongNoAfterX)
        Debug.Print "number after x till dot : " & IIf(InStr(splitLongNoAfterX(d), ".") = 0, splitLongNoAfterX(d), Replace(Mid(splitLongNoAfterX(d), 1, InStr(splitLongNoAfterX(d), ".")), ".", ""))
    Next

    thanks sams
    but this is still not solve the problem. because they still show the other digits (958)

    number after x till dot : 958
    number after x till dot : 10
    number after x till dot : 125

  17. #17
    Lively Member
    Join Date
    Mar 2012
    Posts
    113

    Re: [ASK]Read and Separate

    sorry you should start from 1

    Code:
    'read number after x after dot
     For d = 1 To UBound(splitLongNoAfterX)
        Debug.Print "number after x till dot : " & IIf(InStr(splitLongNoAfterX(d), ".") = 0, splitLongNoAfterX(d), Replace(Mid(splitLongNoAfterX(d), 1, InStr(splitLongNoAfterX(d), ".")), ".", ""))
    Next

  18. #18

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    57

    Wink Re: [ASK]Read and Separate

    to All vbForumers: many thanks. and special thanks for Sams
    now, problem solved

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width