Re: Best way to find records in sequences?
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jul 08, 2006
- 518 views
On Sat, 08 Jul 2006 05:33:09 -0700, Evan Marshall <guest at RapidEuphoria.com> wrote: >ZNorQ wrote: >> Pete Lomax wrote: >> > Eused+=1 >> > if Eused>length(Emsgs) then >> > Emsgs&=repeat(0,32) >> > end if >> > Emsgs[Eused]=txt >> > return Eused >> >> I'm having a real hard time understanding the above code. As below, Evan is probably quite right with the simpler approach. However, since you asked, and for general info: Put with trace trace(1) at the top, change 32 to 2, and watch what happens when you run it. In the following, "(3)2" means 32 if you don't change the repeat, 2 if you do. First time in length(Emsgs) is 0, and as 1>0 the if body is executed. After &=repeat, length(Emsgs) is (3)2 and we replace Emsgs[1]. The next (3)1 times it is called, the if condition yields false, but when Eused hits (3)3, the &=repeat makes length(Emsgs) now (6)4, and so on. In many cases I end that sort of code with Emsgs=Emsgs[1..Eused]. >> Well, more the >> use of 'if' and 'repeat'. Why whould this be necessary? As far as I can >> see, the 'if' would ALWAYS be executed as Eused would be larger than the >> length of Emsgs.. Well, unless Emsgs was tampered with manually - but >> that could go in any direction - either shorten the content, or adding to >> it. And why repeat 32 zeroes before adding the actual text? It is probably worth reminding you here of the following point: Emsgs&=repeat(0,32) increases length(Emsgs) by 32 Emsgs=append(Emsgs,repeat(0,32)) increases length(Emsgs) by 1. > ><snip> >> >> Kenneth > >The if statement would setup a sequence called 'Emsgs' with a length of 32. > >Then Eused would be shorter then length(Emsgs). This function wouldn't work >if Eused was greater than 32, after that you would get a subscript error. Not what happens here - I am assuming that Eused is not modified outside function emsg(). > >Wouldn't this be a better way:? > Emsgs = append(Emsgs,txt) Lots of appends can be quite slow. However for this kind of setup stuff we are probably talking less than 1/1000th of a second, so yes it probably is easier and overall generally better to do it that way. However there are many places where the &=repeat(0,nnn) trick can seriously improve performance, eg the readFile() function of Edita. Regards, Pete