Recent

Author Topic: CASE statement with strings.  (Read 33661 times)

Mike James

  • New Member
  • *
  • Posts: 23
CASE statement with strings.
« on: August 27, 2012, 04:01:44 pm »
I find this construct useful in other languages - how about a CASE statement that can select on strings i.e.

case Foo of
    'bar01': {do something}
    'bar02': {do something else}
end;

=mike=-

Knipfty

  • Full Member
  • ***
  • Posts: 230
Re: CASE statement with strings.
« Reply #1 on: August 27, 2012, 04:10:33 pm »
Hi Mike,

This is from the Pascal online manual http://www.freepascal.org/docs-html/ref/refsu47.html

Quote
The constants appearing in the various case parts must be known at compile-time, and can be of the following types : enumeration types, Ordinal types (except boolean), and chars. The case expression must be also of this type, or a compiler error will occur. All case constants must have the same type.

So you can use chars, but not strings in a CASE statement.

I agree it could be useful, but other languages, such as BASIC, are less effcient, and often use variants.

Knipfty
32-bit Lazarus 1.0 FPC 2.6.0, 64-bit Win 7, Advantage DB 10.10

Mike James

  • New Member
  • *
  • Posts: 23
Re: CASE statement with strings.
« Reply #2 on: August 27, 2012, 04:15:34 pm »
Hi Mike,

This is from the Pascal online manual http://www.freepascal.org/docs-html/ref/refsu47.html

Quote
The constants appearing in the various case parts must be known at compile-time, and can be of the following types : enumeration types, Ordinal types (except boolean), and chars. The case expression must be also of this type, or a compiler error will occur. All case constants must have the same type.

So you can use chars, but not strings in a CASE statement.

I agree it could be useful, but other languages, such as BASIC, are less effcient, and often use variants.

Knipfty

True - but I'm looking at it as an extension to the language. As the string used in the selection would be an immutable type then it would not break the rule that it should be known at compile time.

-=mike=-

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6818
  • Debugger - SynEdit - and more
    • wiki
Re: CASE statement with strings.
« Reply #3 on: August 27, 2012, 04:29:02 pm »
This was implemented already and works in fpc 2.6.0

Code: [Select]
{$mode objfpc}
var s: string;
begin
  case s of
    'abc': s := '123';
    '111': s := '';
  end;
end.

Mike James

  • New Member
  • *
  • Posts: 23
Re: CASE statement with strings.
« Reply #4 on: August 27, 2012, 04:38:44 pm »
This was implemented already and works in fpc 2.6.0

Code: [Select]
{$mode objfpc}
var s: string;
begin
  case s of
    'abc': s := '123';
    '111': s := '';
  end;
end.

Excellent - time to upgrade methinks  :D

btw the FPC 2.6.0 reference guide (pdf) makes no mention of this...

-=mike=-

Leledumbo

  • Hero Member
  • *****
  • Posts: 8287
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: CASE statement with strings.
« Reply #5 on: August 27, 2012, 05:36:30 pm »
Quote
btw the FPC 2.6.0 reference guide (pdf) makes no mention of this...
Please report a missing documentation, it's documented somewhere AFAIR (but I can't find it now).

daveinhull

  • Sr. Member
  • ****
  • Posts: 273
  • 1 divided by nothing must still be 1!
Re: CASE statement with strings.
« Reply #6 on: April 28, 2015, 01:28:08 am »
Hi,

I'm just trying to use a case statement with string cases and I'm getting an error.

I'm using a nightly build version FPC Version 3.1.1 (2015/04/16) SVN Revision 48706.
Code: [Select]
       
Case sField Of
        'Final_Invoice' :
           sFilterString := sFilterString + sField + '=''' + comboFilterDropdown.KeyValue + '''' ;
end;

sField is declared as a string and the case elements are strings, but the error says that there is a mismatch, see attachment.

This discussion indicates that strings were allowed in V2.6.0, but unless I'm doing something very silly, I can't get it to compile.

Any help would be appreciated.

Thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

Blaazen

  • Hero Member
  • *****
  • Posts: 3011
  • POKE 54296,15
    • Eye-Candy Controls
Re: CASE statement with strings.
« Reply #7 on: April 28, 2015, 01:45:19 am »
Hello, I guess you use
Code: [Select]
{$mode delphi}
It works here with FPC 3.1.1 and
Code: [Select]
{$mode objfpc}
Lazarus 2.1.0 r64115 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Leledumbo

  • Hero Member
  • *****
  • Posts: 8287
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: CASE statement with strings.
« Reply #8 on: April 28, 2015, 07:27:49 am »
Hello, I guess you use
Code: [Select]
{$mode delphi}
It works here with FPC 3.1.1 and
Code: [Select]
{$mode objfpc}
It works in FPC/ObjFPC mode only. Delphi, TP, MacPas and ISO mode doesn't support it for compatibility.

daveinhull

  • Sr. Member
  • ****
  • Posts: 273
  • 1 divided by nothing must still be 1!
Re: CASE statement with strings.
« Reply #9 on: April 28, 2015, 03:55:34 pm »
Thanks guys,

That sorted it  :D

Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 854
    • Lebeau Software
Re: CASE statement with strings.
« Reply #10 on: May 07, 2020, 06:57:25 pm »
Quote
btw the FPC 2.6.0 reference guide (pdf) makes no mention of this...
Please report a missing documentation, it's documented somewhere AFAIR (but I can't find it now).

https://www.freepascal.org/docs-html/ref/refsu56.html

Quote
Free Pascal allows the use of strings as case labels, and in that case the case variable must also be a string. When using string types, the case variable and the various labels are compared in a case-sensitive way.

Code: Pascal  [Select][+][-]
  1. Case lowercase(OS) of  
  2.  ’windows’,  
  3.  ’dos’   : WriteLn (’Microsoft playtform);  
  4.  ’macos’,  
  5.  ’darwin’ : Writeln(’Apple platform’);  
  6.  ’linux’,  
  7.  ’freebsd’,  
  8.  ’netbsd’ : Writeln(’Community platform’);  
  9. else  
  10.   WriteLn (’Other platform’);  
  11. end;

The case with strings is equivalent to a series of if then else statements, no optimizations are performed.

However, ranges are allowed, and are the equivalent of an

Code: Pascal  [Select][+][-]
  1. if (value>=beginrange) and (value<=endrange) then  
  2.   begin  
  3.   end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

BeniBela

  • Hero Member
  • *****
  • Posts: 770
    • homepage
Re: CASE statement with strings.
« Reply #11 on: May 07, 2020, 08:08:42 pm »

The case with strings is equivalent to a series of if then else statements, no optimizations are performed.


Business as usual

A great idea and then a crap implementation

It should use a trie or at least a hashmap

Thaddy

  • Hero Member
  • *****
  • Posts: 10684
Re: CASE statement with strings.
« Reply #12 on: May 07, 2020, 08:23:46 pm »
A great idea and then a crap implementation
Well, since you are opiniated, I am too: A lousy idea deserves a mediocre implementation at best. Not even that.
Switch to a scripting language.

To illustrate the best optimization available would be a collision free hash. case hashOf('string'|); (I used such techniques, but abandoned them)
Problem is they do not exist in an easily- speed - computable form. Some say if at all.
https://en.wikipedia.org/wiki/Perfect_hash_function
Best I came up with is a cuckoo hash, also available in several forms in the rtl.generics package .
« Last Edit: May 07, 2020, 08:44:09 pm by Thaddy »

winni

  • Hero Member
  • *****
  • Posts: 2121
Re: CASE statement with strings.
« Reply #13 on: May 07, 2020, 08:33:10 pm »

A great idea and then a crap implementation


I waited over 30 years for that feature.
I love it.

So I don't care about the implementation.

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 10684
Re: CASE statement with strings.
« Reply #14 on: May 07, 2020, 08:45:13 pm »
As long as you grasp that it is slow by definition, that's fine with me, Winni.

 

TinyPortal © 2005-2018