Results 1 to 18 of 18

Thread: [RESOLVED] Read a line with multiple numbers as an array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Resolved [RESOLVED] Read a line with multiple numbers as an array

    I have to read a line which has the data as follows:
    Code:
          1.0   1.0   70.0  70.0
    Now, I want to read that line as an array.

    I have already defined the name of array as following.

    Code:
    Dim D50() as Double
    I now there will be four numbers that I am interested.

    Code:
    Dim numb as Integer
    numb = 4
    ReDim D50(numb)
    Then I tried to read as follows:

    Dim strline as String
    strline = " 1.0 1.0 70.0 70.0"

    What is the best way to assign D50 to those 4 numbers ?

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read a line with multiple numbers as an array

    Well, if you're willing to settle for a Variant rather than a Double array, you can go:

    Code:
    Dim v As Variant
    v = array(1.0,1.0,70.0,70.0)
    And then if you declare the literals, it should be a variant array of doubles:

    Code:
    Dim v As Variant
    v = array(1.0#,1.0#,70.0#,70.0#)
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Re: Read a line with multiple numbers as an array

    I don't want to declare the array but I want to read it.

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read a line with multiple numbers as an array

    If you just really must have an array of double rather than a variant array with doubles in it, and you're unwilling to assign each one separately, then you'd need to write a string parser. This would be a bit of a pain, but not really that big of a deal. I'll let someone else do that though, or I might do it later if nobody else does.

    I still miss the old "Data" and "Read" statements for anyone who even remembers what they are. They would do exactly what cooljd wants.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read a line with multiple numbers as an array

    Ahhh, I just saw your reply. Well, you still need to declare a variable that it will be read into. As stated above, you could do a string parser. If you wanted to use a dynamic array, you'd need to make two passes. One to count the elements, and then Redim your array, then the second pass to put the data in the array.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read a line with multiple numbers as an array

    Here, I'll knock out a rough string parser:

    Code:
    Dim i as long
    Dim t as string
    Dim cnt as long
    Dim d50() as double
    
    t = trim$(s)
    i = instr(t, " ")
    do while i <> 0
        cnt = cnt + 1
        t = trim$(mid$(t, i+1))
        i = instr(t, " ")
    loop
    Redim d50(1 to cnt)
    
    t = trim$(s)
    i = instr(t, " ")
    cnt = 0
    do while i <> 0
        cnt = cnt + 1
        d50(cnt) = val(left$(t, i-1))
        t = trim$(mid$(t, i+1))
        i = instr(t, " ")
    loop
    Something like that. I didn't debug. In fact, I just typed it out here. I'll let others debug it, or it'd be good for you if you did.

    EDIT: I can tell you one situation that would crash it and that'd be an empty "s" string coming in. Notice I didn't supply the "s" string. That's your job. Also, you may want to make sure that cnt is non-zero at the finish of the first loop through.

    EDIT2: Ahhh, there's definitely one bug besides an empty "s" string coming in, but I'm not telling. C programmers tend to call it a "corners" problem, or you may call it a terminating problem. That's all the hint I'll give you.
    Last edited by Elroy; Nov 10th, 2014 at 05:48 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Read a line with multiple numbers as an array

    Quote Originally Posted by cooljd View Post
    What is the best way to assign D50 to those 4 numbers ?
    There are a number or parsing techniques.. Split comes to mind, but it has pitfalls if your line contains double (or more) spaces....

    I 'll write a parsing routine for you to munch on.

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Read a line with multiple numbers as an array

    I would split the string and then assign the return to a double array.
    Code:
    Private Sub Command1_Click()
    Dim strLine As String
    Dim D50() As Double
    Dim i As Integer
    
        strLine = "1.0 1.0 70.0 70.0"
        D50 = StringToDoubleArray(strLine, " ")
        
        For i = 0 To UBound(D50)
            Debug.Print D50(i)
        Next i
    End Sub
    
    
    Private Function StringToDoubleArray(ByVal InputString, ByVal Delimiter) As Double()
    Dim arrString() As String
    Dim arrRet() As Double
    Dim i As Integer
    Dim ub As Integer
    
        arrString = Split(InputString, Delimiter)
        ub = UBound(arrString)
        ReDim arrRet(ub)
        
        For i = 0 To ub
            arrRet(i) = CDbl(arrString(i))
        Next i
        
        StringToDoubleArray = arrRet
    End Function

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read a line with multiple numbers as an array

    Yeah, I don't know why I don't use "Split" more. If you're careful with your delimiters, it's a nice function.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Re: Read a line with multiple numbers as an array

    Quote Originally Posted by MarkT View Post
    I would split the string and then assign the return to a double array.
    Code:
    Private Sub Command1_Click()
    Dim strLine As String
    Dim D50() As Double
    Dim i As Integer
    
        strLine = "1.0 1.0 70.0 70.0"
        D50 = StringToDoubleArray(strLine, " ")
        
        For i = 0 To UBound(D50)
            Debug.Print D50(i)
        Next i
    End Sub
    
    
    Private Function StringToDoubleArray(ByVal InputString, ByVal Delimiter) As Double()
    Dim arrString() As String
    Dim arrRet() As Double
    Dim i As Integer
    Dim ub As Integer
    
        arrString = Split(InputString, Delimiter)
        ub = UBound(arrString)
        ReDim arrRet(ub)
        
        For i = 0 To ub
            arrRet(i) = CDbl(arrString(i))
        Next i
        
        StringToDoubleArray = arrRet
    End Function
    Thank you so much for this example . How about if the spacing is more than 2 or three ? The function may not work properly. What is an easy fix ? If strline looks like following:

    Code:
    strLine = "    1.0   1.0 70.0     70.0"

  11. #11
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Read a line with multiple numbers as an array

    It goes like this

    Code:
    Sub doubleParse(astring$, dbl#())
    ReDim dbl#(20)
    doublecount = 0
    arg = Split(astring$, " ")
    For i = 0 To UBound(arg)
        If Len(arg(i)) Then
            dbl#(doublecount) = CDbl(arg(i))
            doublecount = doublecount + 1
            If doublecount = UBound(dbl) Then
                newub = UBound(dbl) + 20
                ReDim Preserve dbl#(newub)
            End If
        End If
    Next
    ReDim Preserve dbl#(doublecount - 1)
    End Sub


    Code:
    astring$ = " 12.0  12.0  34.5       45.6    57.0   "
    
    ReDim dblarray#(0)
    doubleParse astring$, dblarray#()
    
    rem array packed tight on return
    MsgBox UBound(dblarray#)

  12. #12
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Read a line with multiple numbers as an array

    Quote Originally Posted by Elroy View Post
    Yeah, I don't know why I don't use "Split" more. If you're careful with your delimiters, it's a nice function.
    Quite the opposite for me, never used it much in the past, but since writing text parsing routines, I put it in all recipes, numeric or string.
    Last edited by Navion; Nov 10th, 2014 at 06:22 PM.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    31

    Re: Read a line with multiple numbers as an array

    Quote Originally Posted by Navion View Post
    It goes like this

    Code:
    Sub doubleParse(astring$, dbl#())
    ReDim dbl#(20)
    doublecount = 0
    arg = Split(astring$, " ")
    For i = 0 To UBound(arg)
        If Len(arg(i)) Then
            dbl#(doublecount) = CDbl(arg(i))
            doublecount = doublecount + 1
            If doublecount = UBound(dbl) Then
                newub = UBound(dbl) + 20
                ReDim Preserve dbl#(newub)
            End If
        End If
    Next
    ReDim Preserve dbl#(doublecount - 1)
    End Sub


    Code:
    astring$ = " 12.0  12.0  34.5       45.6    57.0   "
    
    ReDim dblarray#(0)
    doubleParse astring$, dblarray#()
    
    rem array packed tight on return
    MsgBox UBound(dblarray#)
    Thank you so much for your answer. Your function works regardless of any spacings in the string. It seems to be the perfect solution among the ones that have been proposed so far.

  14. #14
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read a line with multiple numbers as an array

    I hope he gets an "A" Navion. *laughing*
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Read a line with multiple numbers as an array

    You're welcomed. I could have written it as a Function but I always go for readability. Written as a Sub, you never have to go lookup some other piece of code to make sure what the types are.

  16. #16
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    557

    Re: Read a line with multiple numbers as an array

    Quote Originally Posted by Elroy View Post
    I hope he gets an "A" Navion. *laughing*
    I get it you pardon my french ? Excusez-moi

  17. #17
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Read a line with multiple numbers as an array

    Quote Originally Posted by cooljd View Post
    Thank you so much for this example . How about if the spacing is more than 2 or three ? The function may not work properly. What is an easy fix ? If strline looks like following:

    Code:
    strLine = "    1.0   1.0 70.0     70.0"
    updating the function like this would take care of it
    Code:
    Private Function StringToDoubleArray(ByVal InputString, ByVal Delimiter) As Double()
    Dim arrString() As String
    Dim arrRet() As Double
    Dim i As Integer
    Dim ub As Integer
    
        InputString = Trim(InputString)
        Do While InStr(InputString, "  ")
            InputString = Replace(InputString, "  ", " ")
        Loop
        
        arrString = Split(InputString, Delimiter)
        ub = UBound(arrString)
        ReDim arrRet(ub)
        
        For i = 0 To ub
            arrRet(i) = CDbl(arrString(i))
        Next i
        
        StringToDoubleArray = arrRet
    End Function

  18. #18
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: [RESOLVED] Read a line with multiple numbers as an array

    To add a short Variant (using a Variant <g>) to the pile:

    Code:
    Sub GetDoubles(S As String, D() As Double)
    Dim n As Long, V
      For Each V In Split(S, " ")
        If Len(V) Then ReDim Preserve D(n): D(n) = V: n = n + 1
      Next
    End Sub
    Olaf

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