Posts: 142
Threads: 6
Joined: Aug 2001
Code:
' Declarations
Declare Sub Split( value As String, result() As String )
' Constants
Const FALSE = 0
Const TRUE = Not False
' Check the string splitter
Dim result() As String
Call Split( "this is a test", result() )
Print result(0)
Print result(1)
Print result(2)
' Splits a string into substrings
Sub Split( value As String, result() As String )
Dim currentcharacter As String
Dim currentstring As Byte
Dim whitespace As Byte
For index = 1 To Len( value )
' Read in a character
currentcharacter = Mid$( value, index, 1 )
Select Case currentcharacter
Case Chr$(32)
' When the first whitespace is encountered, then it's time to resize result() and start another line
If whitespace = FALSE Then
currentstring = currentstring + 1
ReDim Preserve result( currentstring ) As String
End If
' Keeps it from resizing on encountering any whitespace after the first character of whitespace
whitespace = TRUE
Case Else
' Reset the whitespace indicator
whitespace = FALSE
' Add the current character to the current line in result()
result( currentstring ) = result( currentstring ) + currentcharacter
End Select
Next
End Sub
The problem I'm having is that the above code is not putting "this" into result(0). Is this a problem with redim?
Posts: 602
Threads: 27
Joined: Oct 2004
resizing an array means it has to be declared as dynamic like
'$dynamic
dim result() as string
and i dunno wheter you can redim a non global array within a function by only passing it's reference (should work i guess)
quote="NecrosIhsan"]
[/quote]
Posts: 922
Threads: 15
Joined: Jun 2003
Actually, your code is buggy ;)
You must allocate space for result before using it:
Code:
...
Dim whitespace As Byte
redim result(0) as string
Oh yeah, as there's no runtime bound-checking, "result( currentstring ) = result( currentstring ) + currentcharacter" won't be catch by FB.. but Windows should show an expection.. that XP uses to hide.
Posts: 142
Threads: 6
Joined: Aug 2001
Quote:and i dunno wheter you can redim a non global array within a function by only passing it's reference (should work i guess)
I always did that under QB. Nothing ever stopped it.
Quote:You must allocate space for result before using it
I did. Try looking at the code again. In particular, right after the constants are defined.
Quote:Oh yeah, as there's no runtime bound-checking, "result( currentstring ) = result( currentstring ) + currentcharacter" won't be catch by FB.. but Windows should show an expection.. that XP uses to hide.
I have Windows XP SP2. It runs without any exceptions. As a matter of fact, it will crash if I don't redim and Windows will ask if I want to report the problem.
Did either of you try running the code? Obviously not. I've put the results of running the compiled code below.
Code:
C:\Documents and Settings\Michael\Desktop\AutoHeader>autoheader
(null)
is
a
Posts: 922
Threads: 15
Joined: Jun 2003
Whoa, everybody report me tons of bugs and when i say a single test is buggy people get upset? Nevermind..
"Dim result() As String" "()" allocates nothing, see it now?
And of course i tried it, how could i find where the bug was anyway?
Code:
' Declarations
Declare Sub Split( value As String, result() As String )
' Constants
Const FALSE = 0
Const TRUE = Not False
' Check the string splitter
Dim result() As String
Call Split( "this is a test", result() )
Print result(0)
Print result(1)
Print result(2)
' Splits a string into substrings
Sub Split( value As String, result() As String )
Dim currentcharacter As String
Dim currentstring As Byte
Dim whitespace As Byte
REDIM result(0) as String '' <--------- HERE
For index = 1 To Len( value )
' Read in a character
currentcharacter = Mid$( value, index, 1 )
Select Case currentcharacter
Case Chr$(32)
' When the first whitespace is encountered, then it's time to resize result() and start another line
If whitespace = FALSE Then
currentstring = currentstring + 1
ReDim Preserve result( currentstring ) As String
End If
' Keeps it from resizing on encountering any whitespace after the first character of whitespace
whitespace = TRUE
Case Else
' Reset the whitespace indicator
whitespace = FALSE
' Add the current character to the current line in result()
result( currentstring ) = result( currentstring ) + currentcharacter
End Select
Next
End Sub
And the result is:
Code:
C:\prg\code\bas\FREEBA~1>fbc test.bas
C:\prg\code\bas\FREEBA~1>test.exe
this
is
a
Just for fun i compiled that same test (w/o the REDIM i added) with VBDOS, the result was:
Code:
C:\prg\code\bas\FREEBA~1>test.exe
is
a
Surprised? Relax dude..
Posts: 142
Threads: 6
Joined: Aug 2001
Quote:Whoa, everybody report me tons of bugs and when i say a single test is buggy people get upset? Nevermind..
"Dim result() As String" "()" allocates nothing, see it now?
Surprised? Relax dude..
Sorry about yelling at you. It's just that I haven't used QB in a while and I thought my code was correct. :oops:
Quote:"Dim result() As String" "()" allocates nothing, see it now?
Oh and about that. I had put a zero in between the parenthesis at one point, just hadn't tried it within the subroutine.
Thanks for finding the "problem" btw. That thing was driving me crazy. Tried everything
but the way you showed me. :wink: