|
-
Nov 10th, 2014, 04:53 PM
#1
Thread Starter
Junior Member
[RESOLVED] Read a line with multiple numbers as an array
I have to read a line which has the data as follows:
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 ?
-
Nov 10th, 2014, 05:28 PM
#2
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.
-
Nov 10th, 2014, 05:30 PM
#3
Thread Starter
Junior Member
Re: Read a line with multiple numbers as an array
I don't want to declare the array but I want to read it.
-
Nov 10th, 2014, 05:34 PM
#4
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.
-
Nov 10th, 2014, 05:36 PM
#5
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.
-
Nov 10th, 2014, 05:42 PM
#6
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.
-
Nov 10th, 2014, 05:58 PM
#7
Re: Read a line with multiple numbers as an array
 Originally Posted by cooljd
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.
-
Nov 10th, 2014, 06:00 PM
#8
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
-
Nov 10th, 2014, 06:05 PM
#9
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.
-
Nov 10th, 2014, 06:15 PM
#10
Thread Starter
Junior Member
Re: Read a line with multiple numbers as an array
 Originally Posted by MarkT
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"
-
Nov 10th, 2014, 06:16 PM
#11
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#)
-
Nov 10th, 2014, 06:19 PM
#12
Re: Read a line with multiple numbers as an array
 Originally Posted by Elroy
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.
-
Nov 10th, 2014, 06:24 PM
#13
Thread Starter
Junior Member
Re: Read a line with multiple numbers as an array
 Originally Posted by Navion
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.
-
Nov 10th, 2014, 06:40 PM
#14
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.
-
Nov 10th, 2014, 06:41 PM
#15
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.
-
Nov 10th, 2014, 06:44 PM
#16
Re: Read a line with multiple numbers as an array
 Originally Posted by Elroy
I hope he gets an "A" Navion. *laughing* 
I get it you pardon my french ? Excusez-moi
-
Nov 10th, 2014, 09:04 PM
#17
Re: Read a line with multiple numbers as an array
 Originally Posted by cooljd
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
-
Nov 10th, 2014, 09:21 PM
#18
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|