[bb] Get vars from a string by neos300 [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : Get vars from a string
Author : neos300
Posted : 1+ years ago

Description : This is a very simple string parsing system. It simply takes the string and gets the vars from it and jumbles it up. For now it only supports 1 var a string, and you must use spaces to separate things. Also, if you want to have a var at the beginning and then some text then you must start the string with a "".

Examples:(mint = "Flick" btw)

in: "Hi! I like pie!"
out: Hi! I like pie!

in:mint
out:Flick

in:"" + mint + " is a donkey"
out:Flick is a donkey

in: "Hi " + mint + "!"
out: Hi Flick!

in:"Hi " + mint + "! How are you?"
out:"Hi Flick! How are you?

Please replace the GetEnv lines with your own way of getting variables, that was just a test


Code :
Code (blitzbasic) Select
Function StrVars$(p$)
Local noleft$,in%,rest$,space%,var$,kip$, retstr$
If Left(p$, 1) = dqt$
noleft = Right(p$, Len(p) - 1)
;Print noleft
in = Instr(noleft, dqt$)
;Print in
If in <> 0
retstr = retstr + Left(noleft, in - 1)
;Print retstr
If in <> Len(noleft)
in = Instr(noleft, "+")
rest = Mid(noleft, in, Len(noleft))
;Print rest
If Left(rest, 1) = "+"
rest = Right(rest, Len(rest) - 2)
space = Instr(rest, " ", 1)
If space <> 0
var = Left(rest,space-1)
kip = Mid(rest,space+1)
EndIf
;Print var
;Print kip
retstr = retstr + GetEnv(var)
;Print retstr
If Left(kip, 1) = "+"
rest = Right(kip, Len(kip) - 1)
rest = Trim(rest)
rest = Left(rest, Len(rest) - 1)
rest = Right(rest, Len(rest) - 1)
;Print rest
retstr = retstr + rest
EndIf
EndIf
Else
Return Left(noleft, Len(noleft) - 1)
EndIf
Else
AddError("No ending " + dqt$ + "!", "StrVars")
EndIf
Else
Return GetEnv$(p$)
EndIf
Return retstr
End Function


Comments :


RifRaf(Posted 1+ years ago)

 whats wrong with this
mint$="Flick"
newvar$="Hi "+mint$+" how are you ? "

mynum=102
newvar$="I have "+str$(mynum)+" rocks!"



neos300(Posted 1+ years ago)

 At the moment you need spaces bewtween the + and " and var names. I am working on it though


Ked(Posted 1+ years ago)

 Why do you need a function for this when it can be done without a function?Global name$="Bob"
Print "Hello, "+name



RifRaf(Posted 1+ years ago)

 is there an echo in here ? :)


Ked(Posted 1+ years ago)

 Oh, that's what you meant? I thought you were meaning the code wasn't working because you didn't have the needed spaces, like neos300 said.


neos300(Posted 1+ years ago)

 Ked: This is a resource for scripting languages.


_PJ_(Posted 1+ years ago)

 The idea of scripting languages are for scripts to be compiled to allow for "external" code to runalongside and interact with the runtime engine.it looks to me that this is just a string parser which is not only slow (the "script language" is not compiled at all, and string manipulation is relatively slow anyway)  and there's noindication of just how this is to integrate with a compiled exe.


Pineapple(Posted 1+ years ago)

 What's wrong with Replace$()? [/i]