parse a string

BlitzMax Forums/BlitzMax Beginners Area/parse a string

hub(Posted 2007) [#1]
Hi !

a$ = "v=1.0,a=100,t=120,a=200,t=180,r=200,t=30,r=200"

i've the previous string and want parse it as follow :

when i read 'a = 100' i want execute the function acceleration(100), when i read t=120 the function turn (120), ...

before i begin to code this could you advert me on the best algo and usefull blitzmax functions to use ?

Thanks !


SebHoll(Posted 2007) [#2]
I'd split the string into separate instructions, and then split the instruction into the function and the value. You could code your own function to split strings or use this one from the code archives. As seyhajin said, BlitzMax has these functions already built-in...

For example:



You can add as many functions as you want to interpret, simply by adding another Case to the Select block.

Edit: I've added a few comments to let you know what's happening.

Edit 2: Updated so that it uses the built-in BlitzMax string methods.


FlameDuck(Posted 2007) [#3]
Search the code archives for String Tokenizer.


hub(Posted 2007) [#4]
Many thanks for your help. I'll use this code into my game editor. So the editor user could describe/add some paths to the ennemies ships.

Concept code :



CS_TBL(Posted 2007) [#5]
You have hard links to functions there, how about using the event system and just send out an event and have functions that listen to that event? That way you could add more commands without ever needing to update that Select-case-EndSelect part again.


hub(Posted 2007) [#6]
This seems to be a good idea but i'm not familiar with the event system. So i don't know where to start to implement this !


CS_TBL(Posted 2007) [#7]
SuperStrict

Type Ttest
	Field a:Int=1
	Field b:Int=2
	Field c:Float=3.5

	Field newevent:TEvent=New TEvent
	
	Function eventhook:Object(id:Int,data:Object,context:Object)
		If Ttest(context) Ttest(context).ev TEvent(data);Return data	
	EndFunction
	
	Method New()
		AddHook EmitEventHook,eventhook,Self
	End Method
	
	Method Free()
		RemoveHook EmitEventHook,eventhook
		GCCollect()
	End Method
	
	Method ev(event:TEvent)
		If event.id=$999666
			newevent.id=$999555
			newevent.extra=Self
			EmitEvent newevent
		EndIf
	End Method
	
End Type

Function test2:Object(id:Int,data:Object,context:Object)
	Local ev:TEvent=TEvent(data)
	
	If ev.id=$999555
		Local p:Ttest=Ttest(ev.extra)
	
		DebugLog p.a
		DebugLog p.b
		DebugLog p.c
	EndIf
	
	Return data
EndFunction

AddHook EmitEventHook,test2

Local t:Ttest=New Ttest

Local a:TEvent=New TEvent

a.id=$999666
EmitEvent a
End


Some older code from me, from a topic elsewhere. Study it, it shows you how to send a type to a function through an event.


Grey Alien(Posted 2007) [#8]
Yowzer, I don't trust events to arrive regularly or in order, but perhaps I'm just suspicious. I'd much rather have control that hand it over to Window's Events!


hub(Posted 2007) [#9]
it's an interesting approach, nice reading, but as Grey say. i prefer stay on my basic solution.


CS_TBL(Posted 2007) [#10]
Yowzer, I don't trust events to arrive regularly or in order, but perhaps I'm just suspicious.

Got any proof of that?? I figured the whole OS works event-based already, and most people would prefer event-based programming because of its flexibility and non-hardwired'ness.


Dreamora(Posted 2007) [#11]
And because it stops sucking out notebook accu cells like mainloop non-delay based applications, ie it follows the guidelines of Microsoft, Apple and Intel (don't know about AMD, but most likely they suggest events as well)


seyhajin(Posted 2007) [#12]
BRL have integrated two new no documented String Method : Split and Join

Prototypes :
Method Split:String[]( separator$ )
Method Join:String( bits:String[] )

Example of use :
Local s$ = "Hello world"

Local ssplit$[] = s.split(" ")

For Local t$ = EachIn ssplit
	Print t$
Next

Local j$
Print j.Join(ssplit)



daaan(Posted 2007) [#13]
Hey hub. I wrote up a solution for you and posted it in the code archive.

Grab it here:

http://www.blitzbasic.com/codearcs/codearcs.php?code=2101


hub(Posted 2007) [#14]
Many thanks for this !


SebHoll(Posted 2007) [#15]
BRL have integrated two new no documented String Method : Split and Join

Prototypes :
Method Split:String[]( separator$ )
Method Join:String( bits:String[] )


I didn't know that!!! Thanks for pointing them out - just another example of how poorly documented BlitzMax is. :-(


degac(Posted 2007) [#16]

BRL have integrated two new no documented String Method : Split and Join

Prototypes :
Method Split:String[]( separator$ )
Method Join:String( bits:String[] )


Doh!
When? Damn....


Dreamora(Posted 2007) [#17]
During 1.24 through syncmod


dmaz(Posted 2007) [#18]
SuperStrict

Local d:String = ","
Local joined:String = d.join(["apple","cherry","pear"])
Print joined

For Local s:String = EachIn joined.split(",")
	Print s
Next



dmaz(Posted 2007) [#19]
duh, better...
Local joined:String = ",".join(["apple","cherry","pear"])



Filax(Posted 2007) [#20]



fredborg(Posted 2007) [#21]
Local s$ = "Hello|world|This is|a waste of time|For|Sure"
Local t$[] = s.split("|")
Print t[0]
Print t[3]



Filax(Posted 2007) [#22]
Tks Fredborg :)