×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

seperating a delimited string into a dynamic array

seperating a delimited string into a dynamic array

seperating a delimited string into a dynamic array

(OP)
I am looking for a piece of code to help me seperate a comma delimited string into a dynamic array.

For example:
inputting a string of comma delimeted characters into a edit box on a form then seperate the string into the sepperated characters.
 text entered equals "abcd,gfskfs,asdfwe,llooll,foasld"
this would then need to be entered into a dynamic array of type string as seperate strings.
ie
abcd
gfskfs
asdfwe
llooll
foasld

Problem the delimited text string can be of an unknown length so the string array would need to be redimmed.

How do i seperate the delimited string into seperate strings???

This can be done using the string class in VB but does Extra support the string class?

RE: seperating a delimited string into a dynamic array


Hi,

If you were using a better BASIC than Extra has, like Excel VBA, you would have the Split function.

So here's a function you can add to your Excta VB...

CODE

Function SplitIt(s As String, delim As String)
  Dim i As Integer, i1 As Integer, b As String, a() As String, idx As Integer

  i1 = 1
  For i = 1 To Len(s)
    b = Mid(s, i, Len(delim))
    If b = delim Then
        GoSub GrabIt
    End If
  Next
  GoSub GrabIt
  SplitIt = a
  Exit Function
GrabIt:
  ReDim Preserve a(idx)
  a(idx) = Mid(s, i1, i - i1)
  idx = idx + 1
  i1 = i + Len(delim)
  Return
End Function
the function returns an array of each value that is separated by your specified delimiter.
 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: seperating a delimited string into a dynamic array

(OP)
Thanks Skip, Not a very easy way of doing it but i will give it a whirl.

So i would call this like

stringArray() = SplitIt "abcd,gfskfs,asdfwe,llooll,foasld" , ","

Naturally i would not be able to Dim stringArray() or it would only have a max size of 8 elements. or i would have to dim it to an impossibly large size and hope the text entered does not exceed its max size giving me an out of bounds error.

RE: seperating a delimited string into a dynamic array



CODE

dim YourString as string, a as variant, i as integer
YourString = "abcd,gfskfs,asdfwe,llooll,foasld"
a = SplitIt(YourString, ",")
for i = lbound(a) to ubound(a)
 msgbox a(i)
next
or even this

CODE

dim YourString as string, i as integer
YourString = "abcd,gfskfs,asdfwe,llooll,foasld"
for i = lbound(SplitIt(YourString, ",")) to ubound(SplitIt(YourString, ","))
 msgbox SplitIt(YourString, ",")(i)
next
 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: seperating a delimited string into a dynamic array

(OP)
Thanks again Skip

RE: seperating a delimited string into a dynamic array

(OP)
Hmm unfortunately the SplitIt function above does not compile. :( it complaines about improper use of reserved word GoSub...  :(

I will have to think of a work around.

Geez Extra Basic is soo Primitive.   

RE: seperating a delimited string into a dynamic array



I do ALL my Extra manipulation from Excel VBA, since my objective is always data collection from IMS to Excel, or updating IMS from an Excel list.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

RE: seperating a delimited string into a dynamic array

(OP)
Well the work around i got is just repeating the code. once in the loop then again outside of the loop.. Messy i know. but it compiles OK

Function SplitIt(s as String, delim As String)

   Dim i As Integer, i1 As Integer, b as String, a() As String, idx As Integer

   i1 = 1
   For i = 1 to Len(s)
      b = Mid(s,i,Len(delim))
      If b = delim then
         ReDim Preserve a(idx)
         a(idx) = Mid(s, i1, i - i1)
         idx = idx + 1
         i1 = i + Len(delim)
      End If
   Next
   ReDim Preserve a(idx)
   a(idx) = Mid(s, i1, i - i1)
   idx = idx + 1
   i1 = i + Len(delim)
   SplitIt = a    

End Function

The next issue is trying to get the call to the function working. In the first exaple you give,

dim YourString as string, a as variant, i as integer
YourString = "abcd,gfskfs,asdfwe,llooll,foasld"
a = SplitIt(YourString, ",")
for i = lbound(a) to ubound(a)
msgbox a(i)
next

It reports a syntax error . Incorrect or missing Subscript for array a.

The second example complains that SplitIt is not an array. so guess that will not work at all.. I think the first is probably more logically correct.  

RE: seperating a delimited string into a dynamic array

(OP)
Hehe I managed to Get it working if i Declare a() as a Global Variable tho and treat The SplitIt function as one that does not return a Variable.

Although i really hate using global variables.. I think i will have to take what Works.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close