LB Booster
General >> Suggestion Box >> word$(x$,3) = y$
http://lbb.conforums.com/index.cgi?board=suggestions&action=display&num=1491077015

word$(x$,3) = y$
Post by Alincon on Apr 1st, 2017, 8:03pm

LBB allows mid$ on the left side of the equal sign, why not word$?
I would like to do this:

Code:
someRec$ = "name|address|sequence|etc|etc"
seq$ = word$(someRec$,3,"|")
seq = val(seq$)
seq = seq + 1
word$(someRec$,3,"|") = str$(seq)
 


r.m.
Re: word$(x$,3) = y$
Post by Richard Russell on Apr 1st, 2017, 10:04pm

on Apr 1st, 2017, 8:03pm, Alincon wrote:
LBB allows mid$ on the left side of the equal sign, why not word$?

They're really quite different. The crucial thing about assigning to a sub-string is that it's an 'in place' substitution: the length of the string doesn't change, all that happens is that a subset of characters are replaced.

Doing something similar with word$, as you suggest, isn't a straightforward substitution, because (presumably) the 'new' sub-string may be a different length from the 'old' sub-string. For example, in the case of the code you listed, you may wish to replace "99" with "100".

That might mean allocating a new memory block, to accommodate the increased length, copying the old string into it (moving the 'tail' section along one to open up the extra space) and then replacing the "99" with "100".

Hopefully you can see that this is an entirely different kind of operation than a simple assignment to a sub-string. If you accepted a restriction that the new sub-string must be the same length as the old sub-string then it would be possible, but that would be quite limiting.

Richard.

Re: word$(x$,3) = y$
Post by RobM on Apr 2nd, 2017, 12:53am

Quote:
I would like to do this:


Code:
    someRec$ = "name|address|99|etc|etc"
    seq$ = word$(someRec$,3,"|")
    seq = val(seq$)
    seq = seq + 1
    print someRec$
    print ReplaceWord$(someRec$,3,"|",str$(seq))
    end

function ReplaceWord$(source$,index,separator$,new$)
    length=len(source$)
    for x=1 to length
    if mid$(source$,x,1)=separator$ then count=count+1
    next x
    dim temp$(count+1)
    for x=1 to count+1
      temp$(x)=word$(source$,x,separator$)
    next x
    temp$(index)=new$
    for x=1 to count+1
       if x=count+1 then separator$=""
       ReplaceWord$=ReplaceWord$+temp$(x)+separator$
    next x
end function