Re: Best way to find records in sequences?
- Posted by ZNorQ <znorq at holhaug.com> Jul 08, 2006
- 527 views
Pete Lomax wrote: > > On Fri, 07 Jul 2006 04:04:57 -0700, ZNorQ <guest at RapidEuphoria.com> > wrote: > > > MyErrors = { > > {"SYN_SE", "Syntax error" }, > > "ARY_OB", "Out of range" }, > > "MEM_OO", "Out of memory" }, > > "MEM_LD", "Memory leak detected" }, > > "TOB_TM", "Too much tobacco in keyboard"}} > Umm, if you are going to code "SYN_SE" then you may as well code > "Syntax error"... I used a VERY simplified example. I use indices only when the sequences are containing large amounts of data - and usually when they contain several fields for each record. > > MyErrIdx = {"SYN_SE", "ARY_OB", "MEM_OO", "MEM_LD", "TOB_TM"}. > > > >This would ofcourse have to correspond to the same record id as in MyErrors. > ...and that would be almost as fiddly/error-prone to keep in line with > the error text set as using literal integers would. Not at all. I don't do this manually - this is ofcourse created after the MyErrors are declared by a small routine that copies all the indices into MyErrIdx. > > This is what I usually do: > }}} <eucode> > sequence Emsgs > Emsgs={} > integer Eused > Eused = 0 > function emsg(sequence txt) > Eused+=1 > if Eused>length(Emsgs) then > Emsgs&=repeat(0,32) > end if > Emsgs[Eused]=txt > return Eused > end function I'm having a real hard time understanding the above code. 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? > > global constant > E_se = emsg("Syntax error"), > E_oor = emsg("Out of range"), > E_oom = emsg("Out of memory"), > E_mld = emsg("Memory leak detected"), > E_tmkik = emsg("Too much tobacco in keyboard") > > global function getEmsg(integer i) > return Emsgs[i] > end function > </eucode> {{{ > You have to choose a prefix which you don't think will clash with > anything else, eg "E_", "EM_", "EI", etc. and/or use global constants > as per the strings you used, ie SYN_SE etc. While (lots of) global > variables is generally considered a bad thing, constants are not; > it is actually worse to make Emsgs global, since a hidden line such as > Emsgs=prepend(Emsgs,"I am a bug") could cause serious confusion. > Even cryptic names such as the above make code easier to read and > maintain than using literal integers, w/o performance loss. > The initialisation overhead is pretty minimal. > Seems like a good way to do this, the only thing is that it could create alot of constants. In my example, "SYN_SE" is just that - an example. An index could just as well be something readable - like "Syntax error". Remember - the example I use is just an simplified one. My error message list is comprised of more data than just the text. Usually I have a criticallity code, what buttons should be active on the error form, what icon, the heading text, the body text (that could contain alot of information), etc. etc. I do see that the use of constants would have little or no performance loss. I haven't done that much programming, so I couldn't comment on what loss of performance my example would have - that would ofcourse depend on the size of the sequence - as far as I can see. An onther thing is - how criticial is the performance factor - well, that would perhaps depend on what information was in the sequence. For an error message, maybe not so critical - but for a pure "database" containing alot of information in a game... I'm not an expert... :) > Actually, I tend to go one step further and use e01se, e02oor, as a > kind of second-level redundancy check; if I want a sorted list of > error messages I block copy and paste/sort into the documentation. > ) > > HTH, > Pete > > Thanks for your feedback, Pete. It gives me ideas - like PeteS told me - there are several ways to skin a cat.. :) Kenneth