My Assistant
![]() ![]() |
|
|
Apr 12 2012, 06:06 PM
Post
#1
|
|
|
UtterAccess Guru Posts: 641 |
I'm trying to make a 2d array from an array of delimited strings. This is that I have right now:
CODE Function parse(bigstring As String) As String Dim unsplit_arr() As String Dim split_arr() As String Dim tmp_arr() As String Dim x, y As Long unsplit_arr = Split(bigstring, "&") For x = 0 To UBound(unsplit_arr) - 1 'MsgBox unsplit_arr(x) & "|" & x tmp_arr = Split(unsplit_arr(x), "|") split_arr(0, x) = tmp_arr(0) split_arr(1, x) = tmp_arr(1) split_arr(2, x) = tmp_arr(2) split_arr(3, x) = tmp_arr(3) split_arr(4, x) = tmp_arr(4) split_arr(5, x) = tmp_arr(5) split_arr(6, x) = tmp_arr(6) Next parse = split_arr End Function I get a subscript out of range error. |
|
|
|
Apr 12 2012, 06:32 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 17,643 From: Don Mills, ON (Canada) |
You need to use the ReDim statement to define the array once you know how big it's going to be:
CODE Function parse(bigstring As String) As String Dim unsplit_arr() As String Dim split_arr() As String Dim tmp_arr() As String Dim x As Long, y As Long unsplit_arr = Split(bigstring, "&") ReDim split_arr(6, UBound(unsplit_arr) - 1) For x = 0 To UBound(unsplit_arr) - 1 'MsgBox unsplit_arr(x) & "|" & x tmp_arr = Split(unsplit_arr(x), "|") split_arr(0, x) = tmp_arr(0) split_arr(1, x) = tmp_arr(1) split_arr(2, x) = tmp_arr(2) split_arr(3, x) = tmp_arr(3) split_arr(4, x) = tmp_arr(4) split_arr(5, x) = tmp_arr(5) split_arr(6, x) = tmp_arr(6) Next x parse = split_arr End Function Note, too, how I changed your declaration of x and y (although since you don't use y in your code anywhere, you could just leave it out). The way you had them declared, x was a Variant. It's not possible to "short circuit" declarations: you must declare the data type explicitly for each variable, or else it's a Variant. |
|
|
|
Apr 12 2012, 06:56 PM
Post
#3
|
|
|
UtterAccess Guru Posts: 641 |
|
|
|
|
Apr 12 2012, 07:15 PM
Post
#4
|
|
|
UtterAccess VIP Posts: 17,643 From: Don Mills, ON (Canada) |
Sorry: I missed that. (IMG:style_emoticons/default/blush.gif)
Change CODE Function parse(bigstring As String) As String to CODE Function parse(bigstring As String) As Variant |
|
|
|
Apr 12 2012, 07:42 PM
Post
#5
|
|
|
UtterAccess Guru Posts: 641 |
Oh that's what you use variants for (IMG:style_emoticons/default/smile.gif) thanks.
|
|
|
|
Apr 13 2012, 02:22 AM
Post
#6
|
|
|
UtterAccess Ruler Posts: 1,090 |
CODE For x = 0 To UBound(unsplit_arr) - 1 you will be missing 1 array element on the above code should be: CODE For x = 0 To UBound(unsplit_arr)
|
|
|
|
Apr 13 2012, 08:30 AM
Post
#7
|
|
|
UtterAccess Guru Posts: 641 |
Dumb question here, but how do I declare the 2d string array to return this to? right now I just have
Dim info_array() As String |
|
|
|
Apr 13 2012, 09:12 AM
Post
#8
|
|
|
UtterAccess VIP Posts: 17,643 From: Don Mills, ON (Canada) |
That should be sufficient.
|
|
|
|
Apr 13 2012, 09:15 AM
Post
#9
|
|
|
UtterAccess Guru Posts: 641 |
Then how do I send it into a sub? The header I use is Sub writevals(val_list As Variant)
|
|
|
|
Apr 13 2012, 09:53 AM
Post
#10
|
|
|
UtterAccess Guru Posts: 641 |
D'oh. Nevermind....stupid kbd. I need glasses. (IMG:style_emoticons/default/pullhair.gif)
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 24th May 2013 - 10:54 PM |