Re: Append or concatenate to each element in sequence
- Posted by "Christian Cuvier" <Christian.CUVIER at agriculture.gouv.fr> Aug 20, 2004
- 456 views
> Subject: Append or concatenate to each element in sequence > > > posted by: Terry Constant <EUforum at terryconstant.com> > > Is there a better/faster way to concatenate or append to each element > in a sequence than by using a loop, something like illustrated below? > > > }}} <eucode> > sequence s1 > > global function seqConcat(sequence srcSeq, object toAppend) > sequence dstSeq, curElement > > dstSeq = {} > for i = 1 to length(srcSeq) do > curElement = srcSeq[i] & toAppend > dstSeq = append(dstSeq, curElement) > end for > return dstSeq > end function --seqConcat() > > s1 = seqConcat({"abc","def","ghi"}, '\n') > </eucode> {{{ > > > My main concern is speed since I will be doing such operations > on some large sequences. > > Terry Constant Since the length of dstSeq is known, don't waste time with dynamic mallocs. And since your function only knows about a local copy of srcSeq, no need ro do any alloc for a temp sequence that will get copied on return:
sequence s1 global function seqConcat(sequence srcSeq, object toAppend) for i = 1 to length(srcSeq) do srcSeq[i] &= toAppend --so is it to be appended or to concatenated? end for return srcSeq end function --seqConcat() s1 = seqConcat({"abc","def","ghi"}, '\n')
I didn't benchmark it, but there are so many reasons for this to be faster... CChris