Qbasicnews.com

Full Version: substrings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need a program that will take numbers from inbetween .'s

like the 111, 222, and 333 in ".111.222.333"
im just really stumped on this since its late
you can decide not to give me anything and i wont care
ill check in the morning
and ill get it right in the morning

------------------------------------------------------------------
B@x G0rrill@ f0rum
n0t 1337
but a=@ e=3 o=0
I knew this would come in handy one day... look at the "split" function. There's a buggier version of it at the code post. I dont remember if i ever finished it, I think I kept running out of stack space on some of the more recursive functions. *shrug* whatever. I'm pretty sure 'split' works fine. Just say

split "thestring", ".", array()

and it puts all the parts into array(). you can then use val or whatever qb's function is.

Oh, i'm just so happy I havent wasted my time with this. I think I'll recreate those perl array functions too, I'm so happy....

...

naaaah.

Code:
DEFINT A-Z
'$DYNAMIC

REM $STATIC
FUNCTION between$ (text$, tag1$, tag2$, start)
  s = INSTR(start, text$, tag1$) + LEN(tag1$)
  e = INSTR(s, text$, tag2$)
  IF s = 0 OR e = 0 OR s > e THEN between$ = "" ELSE between$ = MID$(text$, s, e - s)
END FUNCTION

FUNCTION join$ (array() AS STRING, inter$)
  joined$ = array(LBOUND(array))
  FOR x = LBOUND(array) + 1 TO UBOUND(array)
    joined$ = joined$ + inter$ + array(x)
  NEXT x
  join$ = joined$
END FUNCTION

FUNCTION replace$ (text$, replacee$, replacement$)
  newtext$ = text$: x = 1

  DO
    x = INSTR(x, newtext$, replacee$)
    IF x <> 0 THEN
      newtext$ = splice$(newtext$, x, x + LEN(replacee$), replacement$)
      x = x + LEN(replacement$) + 1
    END IF
  LOOP UNTIL x = 0
    
  replace$ = newtext$
END FUNCTION

FUNCTION replacebetween$ (text$, replacement$, tag1$, tag2$, start)
newtext$ = text$
s = 1: e = 1

DO UNTIL s = 0 OR e = 0 OR s > e
  s = INSTR(start, newtext$, tag1$)
  e = INSTR(s + LEN(tag1$), newtext$, tag2$)
  IF s <> 0 THEN inbetween$ = MID$(newtext$, s, LEN(newtext$) - e) ELSE EXIT DO
  temptext$ = replacement$
  x = 1

  DO
    x = INSTR(x, temptext$, "\x")
    IF x <> 0 THEN
      temptext$ = splice$(temptext$, x, x + 2, inbetween$)
      x = x + LEN(inbetween$) + 1
    END IF
  LOOP UNTIL x = 0

  newtext$ = splice$(newtext$, s, e + LEN(tag2$), temptext$)
  start = s + 1
LOOP

replacebetween$ = newtext$

END FUNCTION

FUNCTION splice$ (text$, startchar, endchar, text2$)
  IF startchar > 0 THEN newtext$ = newtext$ + LEFT$(text$, startchar - 1)
  newtext$ = newtext$ + text2$
  IF endchar <= LEN(text$) THEN newtext$ = newtext$ + RIGHT$(text$, LEN(text$) - endchar + 1)
  splice$ = newtext$
END FUNCTION

SUB split (text$, splitter$, array() AS STRING)
  REDIM splist(0 TO LEN(text$)) AS INTEGER

  b = 0
  n = 0

  DO
    b = INSTR(b + 1, text$, splitter$)
    IF b <> 0 THEN
      splist(n) = b
      n = n + 1
    END IF
  LOOP UNTIL b = 0

  max = n
  REDIM array(max)

  splitlen = LEN(splitter$)

  n = 0
  FOR x = 1 TO LEN(text$)
    IF x = splist(n) THEN
      n = n + 1
      x = x + splitlen
    END IF

    matchtest = 0
    FOR y = 0 TO max
      IF x = splist(y) THEN matchtest = 1
    NEXT

    IF matchtest = 0 THEN array(n) = array(n) + MID$(text$, x, 1)
  NEXT x

END SUB
Can you please explaine the array thing in there?

BTW how do you canvert a string to a variable
First off, an array is a bunch of variables (of one type) all with the same name, but with different numbers. So instead of having
Code:
firststring1$ = "hi"
secondstring$ = "bob"
thirdstring$ = "hello"
and not being able to access those easily in loops, you can do this:
Code:
strings(1) = "hi"
strings(2) = "bob"
strings(3) = "hello"
and that way you can easily access those variables in FOR...NEXT loops etc.

And a string by nature *is* a variable. So I don't quite understand what you mean by your second question. Sorry.
i meen a string to a number so i can + - / * and so on

and what do i do for the split function where it says array()
Code:
integernum% = VAL(stringnum$)
Put the name of your array.
DIM myvariables(1 TO 10) AS STRING
then where it says array() in his code example, put myvariables() such as this:
Code:
wholestring$ = "5.2.4.1"
split wholestring$, ".", myvariables()

then myvariables(1) = 5,
myvariables(2) = 2,
myvariables(3) = 4, and
myvariables(4) = 1.

Also, if you want to avoid using the VAL() function like I mentioned above, just make myvariables() an INTEGER instead of a STRING like so:
Code:
DIM myvariables(1 TO 10) AS INTEGER
actually, that would make a type mismatch. qb isnt nice and conversion-happy like PHP or Perl, though it's close.

as for how the array thingy works, in qb a function can't return arrays, but you can pass them and use them like references. so you just have to declare the array, as anything.

Code:
dim array(0)
split "111.222.333.444", ".", array

i forget if you need two parenthesis after "array", but that oughta do. the split routine will redim the array to what it needs. to convert those strings to numbers:

Code:
dim values(lbound(array) to ubound(array) as integer

for x = lbound(array) to ubound(array)
    values (x) = val(array(x))
next x

i havent tested this or something, so there might be some flaw i'm missing, but that oughta work.