Inserting code from a string.

Blitz3D Forums/Blitz3D Beginners Area/Inserting code from a string.

Spot(Posted 2003) [#1]
Is there any way to just insert the string$ into my code so that it sets values...

Example:

type player
field x,y,z
end type

p.player=new player

String$= "p\x=1,p\y=2,p\z=5"


Now if I wanted to have string$ (which would set the values for p.player) inserted into my code below this what command would I use?


darklordz(Posted 2003) [#2]
Try
;First Set Type Values
p\x=1
p\y=2
p\z=5
;Now Store in $
String$ = "My String : "+p\y+" "+p\y+" "+p\z



Anthony Flack(Posted 2003) [#3]
Except that's the opposite of what Spot wants...

Extracting the numbers from the string isn't a problem, but AFAIK there isn't any way to reference a variable via a string, so you might have to do it by hand:

select var$
case "p\x": p\x=p\x+value
case "p\y": p\y=p\y+value
case "p\z": p\z=p\z+value



darklordz(Posted 2003) [#4]
Dang..My Bad...
To make it up 2 ya here's this piece of code:
; Set these 2 variables for the Split Function
Dim Word$(1)
Global Max_Word=0

; Our Split Function....
Function Split(String2Parse$,Delimiter$)
	Count=Len(String2Parse$)+1
	Dim word$(Count)
	Max_Word=0 : Start=1
	For t=1To Count
		If Mid(String2Parse$,t,1)=Delimiter$ Or Mid(String2Parse$,t,1)=""
			Max_Word=Max_Word+1
			Word$(Max_Word)=Mid(String2Parse$,Start,t-Start)
			Start=t+1
		EndIf
	Next
End Function
Example Where String$ = "5|6|7"
Split(String$,"|")
For P.Player = Each Player
	P\X = Word$(1)
	P\Y = Word$(2)
	P\Z = Word$(3)
Next


BTW : ColorCoded with my new code formatting tool :)


Anthony Flack(Posted 2003) [#5]
Nice - and nice colours!


Oldefoxx(Posted 2003) [#6]
You might want to consider the string parsing tools of
INSTR() and MID$(). With these, you can separate a proper
string into it's relative parts. Then based in part on
what the string contains, you could make some decisions
about where values are to be assigned. For instance, if
a player's position was being defined by string data in a
text file, you might see something like this:

"p1:x=123.45,y=67.890,z=1023.56,e=525,m=23,t=17"

by using INSTR() to parse for "p", "x", "y", "z", "m", and "t", you can extract the values that follow with the
MI$() function, convert them to a value, and assign them
in a suitable manner:

player(1)\x=123.45
player(1)\y=67.89
player(1)\z=1023.56
player(1)\energy=525
player(1)\missles=23
player(1)\torpedoes=17

Not only can you easily control the number of players and
their capabilities by such tools, you can save partial
games in this manner to restart later, and you can add new
capabilities later if you like. For instance, you decide
you want to add shields, so you add s=200. If "s" for shields is not found in an existing player file, you can
arbitrarily give them a value, such as 100. Then later, when the game is saved, there will be a new category of
"s" so that any changes will become part of future play.

Not that the names of "player", "c", "y", and so on are
merely symbolic. The compiler understands them and makes
memory and register assignments that are consistent with
that symbol. The compiler understands that using "player(1)" will always mean the same reference when the program is created, and that "player(a)" would be the same as
"player(1)" when "a" has a value of "1". But these symbolic names are not a part of the program itself when it is created. So you cannot take a symbolic name, such as
"player", and put it into as string and expect that the program is going to recognize a relationship there. So saying something like "p1:" or "Player1:", in a string that
is accessed by your program, are equally valid (or just as
non-informative if you like). Either one can only be dealt with empirically with IF or SELECT statements, where you
create the decision making effort and tell the computer, via your program, how you want it to respond. A brief
example of what I mean:

While Not Eof(filein)
   a$=ReadString$(filein)
   b=Instr(a$,"p")
   if b then idx=Val(Mid$(a$,b+1))
   b=Instr(a$,"x=")
   if b then player(idx)\x=val(Mid$(a$,b+2))
   b=Instr(a$,"y=")
   If b Then player(idx)\y=val(Mid$(a$,b+2))
   ;more breakouts here
Wend

Function Val#(parm$)
s#=parm$      ;force conversion from string form to float
return s#     ;return the float value
End Function



Spencer(Posted 2008) [#7]
Here is are split function in Blitz3D ... not sure if it's "fast" but it works.

fn_Split(ls_Strng$, ls_Split$, lb_Flush) : This function splits a string based on a delimiter (ls_Split). The delimiter can be any length greater than Zero. The variable lb_Flush (True/False) can be set to true to delete the current colection of objects holding elements of previously split strings.(Type t_StrAry)

fn_ToDim() : This function converts the collection of objects into the global array gs_StrAr(n) where n is the number of objects in the collection and each element within gs_StrAr() is populated with its corresponding element value ( See TEST TWO at the bottom of the CODE example)

fn_Flush() : the Function Deletes all t_StrAry Objects in the collection.

fi_UBound() : Returns the highest t_StrAry Position in the collection. (Starting from 0)

fs_GetEl(li_Elemt) : This function takes an integer as an ID number and returns the string value of the t_StrAry object in the collection with the corresponding i_Pos value.


;********************************************************************************
;---------;---------;---------;---------;---------;---------;---------;---------;
                                                                                ;
Dim gs_StrAr$(1)                                                                ;
                                                                                ;
;----------------------------------------------------------------------         ;
                                                                                ;
Type t_StrAry                                                                   ;
                                                                                ;
    Field i_Pos                                                                 ;
    Field s_Val$                                                                ;
                                                                                ;
End Type                                                                        ;
                                                                                ;
;----------------------------------------------------------------------         ;
                                                                                ;
Function fn_Split(ls_Strng$, ls_Split$, lb_Flush )                              ;
                                                                                ;
    Local li_sPos   = 1                                                         ;
    Local li_ePos   = 0                                                         ;
    Local ls_Token$ = ""                                                        ;
    Local tmp.t_StrAry = Last t_StrAry                                          ;
    Local li_TknCt  = 0                                                         ;
    Local li_SplLn  = Len(ls_Split)                                             ;
    Local li_StrLn  = Len(ls_Strng)                                             ;
                                                                                ;
    If lb_Flush Then                                                            ;
                                                                                ;
        fn_Flush()                                                              ;
                                                                                ;
    ElseIf tmp <> Null Then                                                     ;
                                                                                ;
        li_TknCt = tmp\i_Pos +1                                                 ;
                                                                                ;
    EndIf                                                                       ;
                                                                                ;
                                                                                ;
    If Right(ls_Strng,li_SplLn) <> ls_Split Then                                ;
                                                                                ;
        ls_Strng = ls_Strng + ls_Split                                          ;
                                                                                ;
    EndIf                                                                       ;
                                                                                ;
    Repeat                                                                      ;
                                                                                ;
        li_ePos = Instr(ls_Strng,ls_Split,li_sPos)                              ;
        ls_Token = Mid( ls_Strng, li_sPos, li_ePos - li_sPos )                  ;
                                                                                ;
        n.t_StrAry = New t_StrAry                                               ;
        n\i_Pos = li_TknCt                                                      ;
        n\s_Val = ls_Token                                                      ;
                                                                                ;
        li_TknCt = li_TknCt + 1                                                 ;
        li_sPos  = li_ePos + li_SplLn                                           ;
                                                                                ;
    Until li_sPos > li_StrLn                                                    ;
                                                                                ;
End Function                                                                    ;
                                                                                ;
;----------------------------------------------------------------------         ;
                                                                                ;
Function fn_Flush()                                                             ;
                                                                                ;
    For s.t_StrAry = Each t_StrAry                                              ;
                                                                                ;
        Delete s                                                                ;
                                                                                ;
    Next                                                                        ;
                                                                                ;
End Function                                                                    ;
                                                                                ;
;----------------------------------------------------------------------         ;
                                                                                ;
Function fi_UBound()                                                            ;
                                                                                ;
    Local Tmp.t_StrAry = Last t_StrAry                                          ;
                                                                                ;
    Return Tmp\i_Pos                                                            ;
                                                                                ;
End Function                                                                    ;
                                                                                ;
;----------------------------------------------------------------------         ;
                                                                                ;
Function fn_ToDim()                                                             ;
                                                                                ;
    Local tmp.t_StrAry = Last t_StrAry                                          ;
    Local li_Count = tmp\i_Pos + 1                                              ;
    Local li_tPos  = 0                                                          ;
                                                                                ;
    Dim gs_StrAr$(li_Count)                                                     ;
                                                                                ;
    For s.t_StrAry = Each t_StrAry                                              ;
                                                                                ;
        gs_StrAr(li_tPos) = s\s_Val                                             ;
        li_tPos = li_tPos + 1                                                   ;
                                                                                ;
    Next                                                                        ;
                                                                                ;
End Function                                                                    ;
                                                                                ;
;----------------------------------------------------------------------         ;
                                                                                ;
Function fs_GetEl$(li_Elemt)                                                    ;
                                                                                ;
    Local tmp.t_StrAry = Last t_StrAry                                          ;
    Local li_LastEl = tmp\i_Pos                                                 ;
                                                                                ;
    If li_Elemt <= li_LastEl Then                                               ;
                                                                                ;
        For s.t_StrAry = Each t_StrAry                                          ;
                                                                                ;
           If s\i_Pos = li_Elemt Then                                           ;
                                                                                ;
              Return  s\s_Val                                                   ;
                                                                                ;
           EndIf                                                                ;
                                                                                ;
        Next                                                                    ;
                                                                                ;
        Return ""                                                               ;
                                                                                ;
    Else                                                                        ;
                                                                                ;
        Return ""                                                               ;
                                                                                ;
    EndIf                                                                       ;
                                                                                ;
End Function                                                                    ;
                                                                                ;
;---------;---------;---------;---------;---------;---------;---------;---------;
;********************************************************************************







;---------;---------;---------;---------;---------;---------;---------;---------;

	;---- TEST ONE ----;

	Cls
	Locate 0, 0
	Input("Press Enter to execute TEST ONE...")
	Print
	Print "Test One"
	Print
	
	fn_Split("11;12;13;14;15;16;17;18;19;20;" , ";" , True )
	fn_ToDim()
	
	Print "Element #9 After Array Conversion:" + gs_StrAr(9)
	Print
	Print
	Input("TEST ONE COMPLETE")
	
	
;---------;---------;---------;---------;---------;---------;---------;---------;	
	
	;---- TEST TWO ----;
	
	Cls
	Locate 0, 0
	Input("Press Enter to execute TEST TWO...")
	Print
	Print "Test Two"
	Print
	
	fn_Split("100;200;3000000;40;cool beans!",";",False)
	
	For x.t_StrAry = Each t_StrAry
	
		Print RSet(x\i_Pos,4) + " :" + x\s_Val  
	
	Next
	
	Print
	Print "UBound is :" + fi_UBound()
	Print "Element #2 from Stack :" + fs_GetEl(2)
	Print

	fn_ToDim()
	
	Print "Element #2 from Array after Conversion :" + gs_StrAr(2)
	Print
	Print	
	Input("TEST TWO COMPLETE  [Enter to EXIT]")
	

;---------;---------;---------;---------;---------;---------;---------;---------;
End




Ross C(Posted 2008) [#8]
Should have put this in the code archives :o) Not much help to the guy now ;o) Since it was 4 years ago.


Spencer(Posted 2008) [#9]
Ah good point. I am new to posting. My apologies.