1. Best way to find records in sequences?

Very often I create nested sequences where each record have an unique
index identifying that particular record. Lets say I created a list of
possible error messages in my code;

 MyErrors = 
  {"Syntax error", "Out of memory", "Too much tobacco in keyboard"}

When I want to use a particular error somewhere in the code, I have to
know it's position in the sequence. Now, this error list could be updated
over time as I remember more errors that should have been in it - and
it would be best to add those in the end of the sequence - otherwise the
already existing references to the messages would be wrong.

But, I like to group information together to make it as tidy as possible -
and to make it easy to find it at a later stage. This means that often I
INSERT new elements (in this case - new error messages) inside the sequence,
and not at the end. This ofcourse screws up my code since it would usually
refer to a specific record id to show the correct error.

Lets say I wanted to add a "Memory leak detected" error - I would find a 
suitable place in the sequence;

 MyErrors = {
  {"Syntax error", "Out of memory", "Memory leak detected", 
   "Too much tobacco in keyboard"}}

So - to keep memory messages "grouped", I insert it as the 3rd record. Any
code that refers to MyErrors[3] would now show a memory leak message
instead of "Too much tobacco..". 

Now over to indices - lets do some change to MyErrors;

 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"}}

My problem is that match and find don't work on these, so what I've done
is to make an index table that lists all indices in a "flat" sequence;

  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.

Questions; 
  Do any of you think in the same direction, or am I way off here?
  Is there a better way to do this?

Kenneth

PS! Just in case you where wondering; I do have code that checks if there
are duplicate indices - so that won't be a problem. :)

Oh, by the way, I don't smoke...

new topic     » topic index » view message » categorize

2. Re: Best way to find records in sequences?

ZNorQ wrote:

> My problem is that match and find don't work on these, so what I've done
> is to make an index table that lists all indices in a "flat" sequence;
> 
>   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.
> 
> Questions; 
>   Do any of you think in the same direction, or am I way off here?
>   Is there a better way to do this?
> 
> Kenneth
> 
> PS! Just in case you where wondering; I do have code that checks if there
> are duplicate indices - so that won't be a problem. :)
> 
> Oh, by the way, I don't smoke...

hmmmm - you can eliminate %20 of your code just by purchasing 
a spittoon.   smile smile

Ken Rhodes
Folding at Home: http://folding.stanford.edu/
100% MicroSoft Free
SuSE Linux 10.0
No AdWare, SpyWare, or Viruses!
Life is Good,  smile

new topic     » goto parent     » topic index » view message » categorize

3. Re: Best way to find records in sequences?

Kenneth Rhodes wrote:
> 
> ZNorQ wrote:
> 
> > My problem is that match and find don't work on these, so what I've done
> > is to make an index table that lists all indices in a "flat" sequence;
> > 
> >   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.
> > 
> > Questions; 
> >   Do any of you think in the same direction, or am I way off here?
> >   Is there a better way to do this?
> > 
> > Kenneth
> > 
> > PS! Just in case you where wondering; I do have code that checks if there
> > are duplicate indices - so that won't be a problem. :)
> > 
> > Oh, by the way, I don't smoke...
> 
> hmmmm - you can eliminate %20 of your code just by purchasing 
> a spittoon.   smile smile
> 
> Ken Rhodes
> Folding at Home: <a
> href="http://folding.stanford.edu/">http://folding.stanford.edu/</a>
> 100% MicroSoft Free
> SuSE Linux 10.0
> No AdWare, SpyWare, or Viruses!
> Life is Good,  smile

So in other words - I'm WAY off! :P

new topic     » goto parent     » topic index » view message » categorize

4. Re: Best way to find records in sequences?

> Questions;
>  Do any of you think in the same direction, or am I way off here?
>  Is there a better way to do this?

Search the Archive for Jiri Babor's Associated Tables. Love them and
use them, I do! :)

~Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: Best way to find records in sequences?

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.
blink)

HTH,
Pete

new topic     » goto parent     » topic index » view message » categorize

6. Re: Best way to find records in sequences?

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.
> blink)
> 
> HTH,
> Pete
> 
> 

Thanks for your feedback, Pete. It gives me ideas - like PeteS told me -
there are several ways to skin a cat.. :)

Kenneth

new topic     » goto parent     » topic index » view message » categorize

7. Re: Best way to find records in sequences?

Greg Haberek wrote:
> 
> > Questions;
> >  Do any of you think in the same direction, or am I way off here?
> >  Is there a better way to do this?
> 
> Search the Archive for Jiri Babor's Associated Tables. Love them and
> use them, I do! :)
> 
> ~Greg
> 
> 


Downloaded, and ready for testing!! :)

Thanks, Greg!

Regards,
Kenneth

new topic     » goto parent     » topic index » view message » categorize

8. Re: Best way to find records in sequences?

ZNorQ wrote:
> 
> Pete Lomax wrote:
 <snip>

> 
> > 
> > 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?

<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.  

Wouldn't this be a better way:?

sequence Emsgs
 	  Emsgs={}
 function emsg(sequence txt)
 	Emsgs = append(Emsgs,txt)
 	return length(Emsgs)
 end function


new topic     » goto parent     » topic index » view message » categorize

9. Re: Best way to find records in sequences?

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu