Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-02

case .. with strings.

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

jordi

Hi, can someone help me?

for example, if I have some constants :

const
  name_1 : string [10] = 'abcdeefdre';
  name_2 : string [10] = 'gfsgeyeyey';
  name_3 : string [10] = 'jfmvnvyret';
etc...
Begin
  if paramstr (1) = name_1 then .....;
  if paramstr (1) = name_2 then ......;
  if paramstr (1) = name_3 then ......;
etc...
Is there another way to evaluate this constant strings?
like : case paramstr (1) of
             name_1 : ...;
             name_2 : ....;
             name_3 : .....;
           end;
the if sentence is very clear but if I have a lot of const values
the program looks a little "stupid".
Sorry for my poor english
Salutacions. Jordi. http://galeon.com/jovy215639/
Michael Van Canneyt
to jordi
jordi wrote 24 lines
Hi, can someone help me? for example, if I have some constants : const name_1 : string [10] = 'abcdeefdre'; name_2 : string [10] = 'gfsgeyeyey'; name_3 : string [10] = 'jfmvnvyret'; etc... Begin if paramstr (1) = name_1 then .....; if paramstr (1) = name_2 then ......; if paramstr (1) = name_3 then ......; etc... Is there another way to evaluate this constant strings? like : case paramstr (1) of name_1 : ...; name_2 : ....; name_3 : .....; end; the if sentence is very clear but if I have a lot of const values the program looks a little "stupid".

Case with strings is not supported, and probably will never be supported. One way of doing the above is to

 const
   N = 3;
   names : Array [1..N] string [10] = ('abcdeefdre','gfsgeyeyey','jfmvnvyret');
Var
  I,Which : Integer;
begin
  I:=1;
  Which:=0;
  While (Which=0) and (I<=N) do
    If Names[I]=ParamStr(1) then
      Which:=I
    else
      Inc(I);
  Case Which of
   1 : ;
   2 : ;
   3 : ;
  else
    // Zero, invalid option.
  end
jordi
to Michael Van Canneyt
Michael Van Canneyt wrote 25 lines
Case with strings is not supported, and probably will never be supported. One way of doing the above is to const N = 3; names : Array [1..N] string [10] = ('abcdeefdre','gfsgeyeyey','jfmvnvyret'); Var I,Which : Integer; begin I:=1; Which:=0; While (Which=0) and (I<=N) do If Names[I]=ParamStr(1) then Which:=I else Inc(I); Case Which of 1 : ; 2 : ; 3 : ; else // Zero, invalid option. end

Thanks.

Salutacions. Jordi. http://galeon.com/jovy215639/