[Lazarus] Easiest way to "case" strings

Reenen Laurie rlaurie at gmail.com
Thu Mar 26 12:32:54 CET 2009


Getting back to the original question...

I implemented it with (as Michael van Canneyt suggested)

function wcase(Needle:string;Haystack:array of string):integer;
begin
 Result:=High(HayStack);
 While (Result>=0) and (CompareText(Needle,Haystack[Result])<>0) do
   Dec(Result);
end;

case wcase(s,['network command 1','network command 2','network command
3','network command 4']) of
{'network command 1'} 0 :
{'network command 2'} 1 :
{'network command 3'} 2 :
{'network command 4'} 3 :
end;

It's decent on readability, but not at all great in maintainability.  If I
want to add network command 1.5  (let's say it makes sense to put it there,
because it does something similar to 1 and 2)... then I need to put it in
the array (which if there is 50 network commands can be a LOT), then I need
to add it the case statement.

Obviously in my case, making all my network commands an ordinal type, and
sending the ordinal value through would make the program more efficient etc.
But it's my first networking app, and I want it easy to debug, and human
readable when it comes through the pipe...

I guess usually when one would want to have a string case, it's probably bad
programming in terms of efficiency... and one should rather encourage good
programming practices than optimize bad programming practices with hash
tables/tree lookups.

Regards,
-Reenen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20090326/76c7195b/attachment-0007.html>


More information about the Lazarus mailing list