Author | 
 Topic: word$(x$,3) = y$  (Read 236 times) | 
 
 
 | 
 
 
Alincon
 Full Member
 
  member is offline
  
 
  
    
    
  
Posts: 147 
 | 
  | 
word$(x$,3) = y$ 
« Thread started 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.
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
Richard Russell
 Administrator
 
  member is offline
  
 
  
    
     
  
Posts: 1348 
 | 
  | 
Re: word$(x$,3) = y$ 
« Reply #1 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. 
 | 
 
 | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
RobM
 Junior Member
 
  member is offline
  
 
  
    
    
  
Posts: 91 
 | 
  | 
Re: word$(x$,3) = y$ 
« Reply #2 on: Apr 2nd, 2017, 12:53am » | 
 | 
 
 
 
 Quote:
   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   
 | 
 
| « Last Edit: Apr 2nd, 2017, 03:33am by RobM » | 
  Logged
 | 
               
             
            
             
             | 
           
         
         | 
       
     
     | 
   
 
 
 |