Code archives/Algorithms/Get vars from a string
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
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 | |||||
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
| ||
whats wrong with thismint$="Flick" newvar$="Hi "+mint$+" how are you ? " mynum=102 newvar$="I have "+str$(mynum)+" rocks!" |
| ||
At the moment you need spaces bewtween the + and " and var names. I am working on it though |
| ||
Why do you need a function for this when it can be done without a function?Global name$="Bob" Print "Hello, "+name |
| ||
is there an echo in here ? :) |
| ||
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. |
| ||
Ked: This is a resource for scripting languages. |
| ||
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. |
| ||
What's wrong with Replace$()? |
Code Archives Forum