parse a string
BlitzMax Forums/BlitzMax Beginners Area/parse a string
| ||
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 ! |
| ||
I'd split the string into separate instructions, and then split the instruction into the function and the value. 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. |
| ||
Search the code archives for String Tokenizer. |
| ||
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 : |
| ||
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. |
| ||
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 ! |
| ||
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. |
| ||
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! |
| ||
it's an interesting approach, nice reading, but as Grey say. i prefer stay on my basic solution. |
| ||
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. |
| ||
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) |
| ||
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) |
| ||
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 |
| ||
Many thanks for this ! |
| ||
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. :-( |
| ||
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.... |
| ||
During 1.24 through syncmod |
| ||
SuperStrict Local d:String = "," Local joined:String = d.join(["apple","cherry","pear"]) Print joined For Local s:String = EachIn joined.split(",") Print s Next |
| ||
duh, better...Local joined:String = ",".join(["apple","cherry","pear"]) |
| ||
|
| ||
Local s$ = "Hello|world|This is|a waste of time|For|Sure" Local t$[] = s.split("|") Print t[0] Print t[3] |
| ||
Tks Fredborg :) |