Posts: 3,343
Threads: 83
Joined: Mar 2003
Create an explode and/or implode function in QBasic.
If you're familiar with php or a language like it, the explode function is exactly like the ones you see there.
Here's the php.net description:
Quote:explode
(PHP 3, PHP 4 )
explode -- Split a string by string
Description
array explode ( string separator, string string [, int limit])
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string separator. If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.
If separator is an empty string (""), explode() will return FALSE. If separator contains a value that is not contained in string, then explode() will return an array containing string.
I'm aware that QB can't return arrays from functions, so instead I want:
explode$ (string separator, string string, int part)
So the "part" variable specifies which part to return. Here's an example:
Code: DIM explodedarray$(1 TO 3)
FOR I = 1 TO 3
explodearray$(I) = explode$(" ", stringtoseparate$, I)
NEXT I
The function should return a null string if the part variable is not within the ranges of the parts in the string. so for a string like:
"hello world"
There would be 2 parts, and if explode$ was called with 3 or 0 for example, it would return "".
ALSO: To make this function seem useful, you should try to make another function, explodeparts%(string separator, string string), which returns the number of parts in the string.
Finally, make an implode function that joins an exploded array back together.
You'll get good marks if the explode$, explodeparts% and implode function are compatible, and work as expected.
Geez, that took ages to write
Posts: 1,166
Threads: 62
Joined: Apr 2003
I'm not participating, but that just reminded me of a function SuperGhost gave me a while ago that's been TREMENDOUSLY helpful.
Code: FUNCTION Parse$ (Word$, Sep$, Word%)
L% = 1: S% = 1
W$ = Sep$ + Word$ + Sep$
DO
S% = INSTR(S% + 1, W$, Sep$): IF S% = 0 THEN EXIT DO
SELECT CASE S%
CASE 0: EXIT FUNCTION
CASE IS > 1: Wd% = Wd% + 1
IF Wd% = Word% THEN Parse$ = MID$(W$, L% + LEN(Sep$), S% - L% - LEN(Sep$)): EXIT DO
END SELECT
L% = S%
LOOP
END FUNCTION
am an asshole. Get used to it.
Posts: 788
Threads: 53
Joined: Nov 2002
Since when can't qb pass arrays?
dim myarray(99) as bleh
sub myfunc ( myarray() as bleh )
...
end sub
oship me and i will give you lots of guurrls and beeea
Posts: 1,813
Threads: 284
Joined: Mar 2003
Damn good thing you brought this one up.... I've been looking for these 2 functions in QB for a while.... well not so much looking as just hoping someone might convieniently give them to me. lol
earn.
Posts: 1,813
Threads: 284
Joined: Mar 2003
Quote:Since when can't qb pass arrays?
dim myarray(99) as bleh
sub myfunc ( myarray() as bleh )
...
end sub
He said QB can't return arrays from FUNCTIONs. But passing it to a SUB works just the same way.
earn.
Posts: 90
Threads: 8
Joined: Aug 2003
Separator can be an string? (ie: explode$("of", "one of two of three", 4)
What about if there's no part in the string? (as in the previous example)
And finally what's about the implode function?
Posts: 1,080
Threads: 87
Joined: Feb 2002
well the implode function would be something like
Code: FUNCTION implode(string_array())
DIM st$=""
FOR n = LBOUND(string_array()) TO UBOUND(string_array())
st$=st$+string_array(n)
NEXT
implode=st$
END FUNCTION
This code is highly untested and I have never passed arrays or used
LBOUND/UBOUND before...
(array passing: I guessed from Blitz's example and the
LBOUND/UBOUND I looked up at the QBOHO)
/post]
Posts: 3,343
Threads: 83
Joined: Mar 2003
Quote:Since when can't qb pass arrays?
dim myarray(99) as bleh
sub myfunc ( myarray() as bleh )
...
end sub
I'm well aware of that, but like seph said, a function cannot return an array
xhantt: The separator *has* to be a string
red_marvin: remember to add back in the separator
Posts: 309
Threads: 15
Joined: Jul 2003
I don't know if this is acceptible. I did it a little differently: Code: DECLARE FUNCTION ExplodedParts% (Sep$, Inpt$, Outpt$)
DECLARE FUNCTION Explode$ (Sep$, Inpt$, BYVAL Part%)
DECLARE FUNCTION Implode$ (StrArray() AS STRING, Sep$, BYVAL Parts%)
'--------------- Sample Program -----------------------
' CLS
' Separator$ = "Xy"
' StrngToSep$ = "XyXyoneXyXyXytwoXythreeXyX"
' PRINT StrngToSep$, Separator$
'
' Parts = ExplodedParts(Separator$, StrngToSep$, Refrmtd$)
' DIM StringArray(1 TO Parts) AS STRING
'
' FOR i = 1 TO Parts
' StringArray(i) = Explode$(Separator$, Refrmtd$, i)
' PRINT StringArray(i)
' NEXT
'
' Rebuilt$ = Implode$(StringArray(), Separator$, Parts)
' PRINT Rebuilt$
'
' Notice that the final is not the same as the original
' string. This is because of the reformatting in
' ExplodedParts. If this is undesirable, remove the
' reformatting.
DEFINT A-Z
FUNCTION Explode$ (Sep$, Inpt$, BYVAL Part)
' Input:
' Inpt$ - A string to be broken into substrings.
' (Using Outpt$ from ExplodedParts is
' recommended).
' Sep$ - A possible substring of Inpt$ used to
' determine break points.
' Part - the number of the resulting substring
' to be returned.
' Returns: "False" if Sep$ = "".
' "" if Part is less than 1, or greater
' than the number of substrings.
' A substring otherwise.
IF Sep$ = "" THEN
Explode$ = "False"
ELSEIF Part < 1 THEN
Explode$ = ""
ELSE
Length = LEN(Inpt$)
SepLngth = LEN(Sep$)
Char1 = 1
FOR i = 2 TO Part
Char1 = INSTR(Char1, Inpt$, Sep$) + SepLngth
IF Char1 > Length THEN
Explode$ = ""
EXIT FUNCTION
END IF
NEXT
SubStrLngth = INSTR(Char1, Inpt$ + Sep$, Sep$) - Char1
Explode$ = MID$(Inpt$, Char1, SubStrLngth)
END IF
END FUNCTION
FUNCTION ExplodedParts (Sep$, Inpt$, Outpt$)
' Input:
' Inpt$ - A string to be broken into substrings.
' Sep$ - A possible substring of Inpt$ used to
' determine break points.
' Output:
' Outpt$ - A reformatted version of Inpt$, without
' leading, trailing, or consecutive Sep$'s.
'
' Returns: 0 if Sep$ = "".
' 1 if Sep$ is not an interior substring
' of Inpt$.
' The number of resulting substrings otherwise.
Outpt$ = Inpt$
IF Sep$ = "" OR Inpt$ = "" THEN
ExplodedParts = 0
ELSE
Length = LEN(Outpt$)
SepLngth = LEN(Sep$)
'----- Remove all leading separators ---------
WHILE MID$(Outpt$, 1, SepLngth) = Sep$
Length = Length - SepLngth
Outpt$ = RIGHT$(Outpt$, Length)
WEND
'----- Remove all trailing separators ---------
WHILE MID$(Outpt$, Length + 1 - SepLngth, SepLngth) = Sep$
Length = Length - SepLngth
Outpt$ = LEFT$(Outpt$, Length)
WEND
n = 1
PrevFind = 1 - SepLngth
DO
Find = INSTR(PrevFind + SepLngth, Outpt$, Sep$)
IF Find = 0 THEN
EXIT DO
ELSEIF Find = PrevFind + SepLngth THEN
'-------- delete consecutive separators ------------------
Outpt$ = LEFT$(Outpt$, Find - 1) + RIGHT$(Outpt$, Length - Find - SepLngth + 1)
Length = Length - SepLngth
ELSE
n = n + 1
PrevFind = Find
END IF
LOOP
ExplodedParts = n
END IF
END FUNCTION
FUNCTION Implode$ (StrArray() AS STRING, Sep$, BYVAL Parts)
' Implode$ concatenates the elements of a string array
' with separators, Sep$, between the elements.
' It assumes the first element is StrArray(1).
IF UBOUND(StrArray) < Parts THEN
Implode$ = "Not enough substrings"
ELSE
Working$ = StrArray(1)
FOR Part = 2 TO Parts
Working$ = Working$ + Sep$ + StrArray(Part)
NEXT
Implode$ = Working$
END IF
END FUNCTION
hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)
For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
Posts: 3,343
Threads: 83
Joined: Mar 2003
I'll test it soon (me->linux now)
|