Ooops
November 28, 2020, 02:00:51 AM

Author Topic: [bb] Get vars from a string by neos300 [ 1+ years ago ]  (Read 386 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Get vars from a string by neos300 [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
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
  1. Function StrVars$(p$)
  2. Local noleft$,in%,rest$,space%,var$,kip$, retstr$
  3. If Left(p$, 1) = dqt$
  4.         noleft = Right(p$, Len(p) - 1)
  5.         ;Print noleft
  6.         in = Instr(noleft, dqt$)
  7.         ;Print in
  8.         If in <> 0
  9.                 retstr = retstr + Left(noleft, in - 1)
  10.                 ;Print retstr
  11.                 If in <> Len(noleft)
  12.                         in = Instr(noleft, "+")
  13.                         rest = Mid(noleft, in, Len(noleft))
  14.                         ;Print rest
  15.                         If Left(rest, 1) = "+"
  16.                                 rest = Right(rest, Len(rest) - 2)
  17.                                 space = Instr(rest, " ", 1)
  18.                                 If space <> 0
  19.                                         var = Left(rest,space-1)
  20.                                         kip = Mid(rest,space+1)
  21.                                 EndIf
  22.                                 ;Print var
  23.                                 ;Print kip
  24.                                 retstr = retstr + GetEnv(var)
  25.                                 ;Print retstr
  26.                                 If Left(kip, 1) = "+"
  27.                                         rest = Right(kip, Len(kip) - 1)
  28.                                         rest = Trim(rest)
  29.                                         rest = Left(rest, Len(rest) - 1)
  30.                                         rest = Right(rest, Len(rest) - 1)
  31.                                         ;Print rest
  32.                                         retstr = retstr + rest
  33.                                 EndIf
  34.                         EndIf
  35.                 Else
  36.                         Return Left(noleft, Len(noleft) - 1)
  37.                 EndIf
  38.         Else
  39.                 AddError("No ending " + dqt$ + "!", "StrVars")
  40.         EndIf
  41. Else
  42.         Return GetEnv$(p$)
  43. EndIf
  44. Return retstr
  45. End Function


Comments :


RifRaf(Posted 1+ years ago)

 whats wrong with this
Code: [Select]
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?
Code: [Select]
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]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal