Re: Best way to find records in sequences?
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Jul 07, 2006
- 539 views
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"... > 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. This is what I usually do:
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 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
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. 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