RE: Slicing and Splicing
- Posted by Chris Bensler <bensler at mail.com> Mar 22, 2002
- 457 views
I didn't realize that slice() was redundant. The splice function can be used for both cases. It can be used for inserting or removing elements from the sequence. Chris <CODE> type seq_splice(object s) if sequence(s) then if length(s)=2 and integer(s[1]) and integer(s[2]) then return TRUE end if end if return FALSE end type global function splice(sequence seq, seq_splice indices, object x) return seq[1..indices[1]-1] &x& seq[indices[2]+1..length(seq)] end function sequence s s = "ting" s = splice(s,{2,1}, "est") -- insert at index 2 ( "testing" ) puts(1,s&"\n") s= splice(s,{5,7}, "") -- remove index 5 to 7 ( "test" ) puts(1,s&"\n") s = splice(s,{5}, "ed") -- append ( "tested" ) puts(1,s&"\n") s= splice(s,{5,6}, "imony") -- replace index 5 to 6 ( "testimony" ) puts(1,s&"\n") s= splice(s,{1,2}, "") -- remove index 1 to 2 puts(1,s&"\n") s= splice(s,{1,3}, "har") -- replace index 1 to 3 ( "harmony" ) puts(1,s&"\n") while get_key()=-1 do end while <END OF CODE>