Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array question

Status
Not open for further replies.

vzachin

Technical User
Feb 10, 2006
305
US
hi

i have a line of data with fields of various lengths that i need to extract. each field varies in length and are separated by spaces (could be one or more spaces):
eg: XXXXXXXX XXXXXX XXXXXXXXX XXXXXXXXX XX

i'm looking for a code that will give me something like
field1 = " ", field2 = " "...etc. i need to place these fields on another screen.

i tried to test each character until i reach a space. this is what i have so far
Code:
linedat = sess.getstring (16, 11, 69) 
for i = len(linedat)
field = mid(linedat,i,1)
if field = " " then field1 = sess.getstring (16,11, i -1)
next i
i can get the first field but am stuck getting the rest of the fields.

any thoughts? i'm not good with arrays.

thanks
zach
 

zach,

VBA has a Split function. Extra does not. So here's a clone of that function and an example, written and tested in VBA...
Code:
Function SplitIt(s As String, delim As String)
    Dim i As Integer, a() As String, p1 As Integer, idx
    
    p1 = 1
    For i = 1 To Len(s)
        If Mid(s, i, 1) = delim Then
            ReDim Preserve a(idx)
            a(idx) = Mid(s, p1, i - p1)
            p1 = i + 1
            idx = idx + 1
        End If
    Next
    SplitIt = a
End Function
Sub testit()
    Dim a, i As Integer
    a = SplitIt("XXXXXXXX   XXXXXX XXXXXXXXX XXXXXXXXX XX", " ")
    For i = 0 To UBound(a)
        Debug.Print a(i)
    Next
End Sub

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


Sorry zach, I failed to add the final parse...
Code:
Function SplitIt(s As String, delim As String)
    Dim i As Integer, a() As String, p1 As Integer, idx
    
    p1 = 1
    For i = 1 To Len(s)
        If Mid(s, i, 1) = delim Then
            ReDim Preserve a(idx)
            a(idx) = Mid(s, p1, i - p1)
            p1 = i + 1
            idx = idx + 1
        End If
    Next
    ReDim Preserve a(idx)
    a(idx) = Mid(s, p1, i - p1)
   SplitIt = a
End Function


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


Here it is cleaned up a bit
Code:
Function SplitIt(s As String, delim As String)
    Dim i As Integer, a() As String, p1 As Integer, idx
    
    p1 = 1
    For i = 1 To Len(s)
        If Mid(s, i, 1) = delim Then
            GoSub ParseIt
            p1 = i + 1
            idx = idx + 1
        End If
    Next
    GoSub ParseIt
    
    SplitIt = a
    Exit Function
    
ParseIt:
    ReDim Preserve a(idx)
    a(idx) = Mid(s, p1, i - p1)
    Return
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
hi skip,
thanks for the feedback. i will give it a try.

zachi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top