Discussion:
Problem with case and string label.
(too old to reply)
Cooper
2010-02-16 11:56:41 UTC
Permalink
Hello, i have this small problem, me need check a string and to second of
content of string take a choose, so i have thinked to this solution (just a
example):

case mystring of
// Action
'dog' : out := 'cane';
'cat': out := 'gatto';
'fish': out := 'pesce';
else
// Error (do something)
end;

Doing so, delphi back me error telling me that required ordinal type. I
suppose that mystring not can to be a string but a integer value and that
cases value need to be integer value too.
But so i can solve this situation? Me have string value and not integer
value.
Thanks for help.
Marco van de Voort
2010-02-16 12:23:59 UTC
Permalink
Post by Cooper
Doing so, delphi back me error telling me that required ordinal type. I
suppose that mystring not can to be a string but a integer value and that
cases value need to be integer value too.
But so i can solve this situation? Me have string value and not integer
value.
1) not use a case but successive ifs
2) map the strings somehow to integers (e.g. table lookup) and do the
case on the resulting integers.
Maarten Wiltink
2010-02-16 12:27:05 UTC
Permalink
Post by Cooper
Hello, i have this small problem, me need check a string and to second
of content of string take a choose, so i have thinked to this solution
case mystring of
// Action
'dog' : out := 'cane';
'cat': out := 'gatto';
'fish': out := 'pesce';
else
// Error (do something)
end;
Doing so, delphi back me error telling me that required ordinal type.
Correct. It's simply a limitation of the case statement. You can for
example do this instead:

if (MyString='dog')
then Out:='cane'
else if (MyString='cat')
then Out:='gatto'
else if (MyString='fish')
then Out:='pesce'
else Error;

Of course, it's not exactly pretty, and I'm not even talking about my
preferences for laying out iterated if-statements. It would be better
to have a list of translations and loop over it looking for a match,
then returning the translation.

Groetjes,
Maarten Wiltink
Cooper
2010-02-16 13:32:27 UTC
Permalink
Post by Maarten Wiltink
Correct. It's simply a limitation of the case statement. You can for
if (MyString='dog')
then Out:='cane'
else if (MyString='cat')
then Out:='gatto'
else if (MyString='fish')
then Out:='pesce'
else Error;
Of course, it's not exactly pretty, and I'm not even talking about my
preferences for laying out iterated if-statements. It would be better
to have a list of translations and loop over it looking for a match,
then returning the translation.
Thanks very much, i try.
Marco van de Voort
2010-02-17 21:06:56 UTC
Permalink
Post by Maarten Wiltink
Post by Cooper
// Error (do something)
end;
Doing so, delphi back me error telling me that required ordinal type.
Correct. It's simply a limitation of the case statement. You can for
if (MyString='dog')
then Out:='cane'
else if (MyString='cat')
then Out:='gatto'
else if (MyString='fish')
then Out:='pesce'
else Error;
Of course, it's not exactly pretty, and I'm not even talking about my
preferences for laying out iterated if-statements. It would be better
to have a list of translations and loop over it looking for a match,
then returning the translation.
I'll forward this discussion to the person that implemented "case <string> of"
in FPC btw ( :-) )
Maarten Wiltink
2010-02-18 10:09:10 UTC
Permalink
<case statements do not do strings>
Post by Marco van de Voort
Post by Maarten Wiltink
Correct. It's simply a limitation of the case statement. You can for
if (MyString='dog')
then Out:='cane'
else if (MyString='cat')
then Out:='gatto'
else if (MyString='fish')
then Out:='pesce'
else Error;
Of course, it's not exactly pretty, and I'm not even talking about my
preferences for laying out iterated if-statements. It would be better
to have a list of translations and loop over it looking for a match,
then returning the translation.
I'll forward this discussion to the person that implemented
"case <string> of" in FPC btw ( :-) )
Is there anything in it he doesn't yet know?

There is nothing exactly _wrong_ with allowing non-ordinal case selector
expressions (and concomitant case labels). The case statement was always
syntactic sugar and an optimisation hint to the compiler.

Groetjes,
Maarten Wiltink
Marco van de Voort
2010-02-18 19:53:05 UTC
Permalink
Post by Maarten Wiltink
Post by Marco van de Voort
Post by Maarten Wiltink
Of course, it's not exactly pretty, and I'm not even talking about my
preferences for laying out iterated if-statements. It would be better
to have a list of translations and loop over it looking for a match,
then returning the translation.
I'll forward this discussion to the person that implemented
"case <string> of" in FPC btw ( :-) )
Is there anything in it he doesn't yet know?
I wouldn't know.
Post by Maarten Wiltink
There is nothing exactly _wrong_ with allowing non-ordinal case selector
expressions (and concomitant case labels). The case statement was always
syntactic sugar and an optimisation hint to the compiler.
Syntactic sugar (and then specially the more rarely used cases) are often
not that wrong on themselves. Together however, they make a baroque
language.
Rudy Velthuis
2010-02-18 20:06:56 UTC
Permalink
There is nothing exactly wrong with allowing non-ordinal case selector
expressions (and concomitant case labels). The case statement was
always syntactic sugar and an optimisation hint to the compiler.
Not only a hint. In the old days of TP, but even today, case statements
can be optimized more easily than an if-then-else ladder. But that is
only true for ordinals, which can easily be grouped together.
--
Rudy Velthuis http://rvelthuis.de

"Violence is the last refuge of the incompetent." -- Issac Asimov
Marco van de Voort
2010-02-20 13:36:20 UTC
Permalink
Post by Rudy Velthuis
There is nothing exactly wrong with allowing non-ordinal case selector
expressions (and concomitant case labels). The case statement was
always syntactic sugar and an optimisation hint to the compiler.
Not only a hint. In the old days of TP, but even today, case statements
can be optimized more easily than an if-then-else ladder. But that is
only true for ordinals, which can easily be grouped together.
There are possibilities for strings too. Hashes, binary search etc. Less
easy, but not a problem for any compiler or hardware of the last 10-13 years.

TP had constraints, sure, but we are also already 15 years past TP now.

I don't think the reasons why it was added are technical.
Rudy Velthuis
2010-02-20 15:33:38 UTC
Permalink
Post by Rudy Velthuis
There is nothing exactly wrong with allowing non-ordinal case
selector >> expressions (and concomitant case labels). The case
statement was >> always syntactic sugar and an optimisation hint to
the compiler.
Post by Rudy Velthuis
Not only a hint. In the old days of TP, but even today, case
statements can be optimized more easily than an if-then-else
ladder. But that is only true for ordinals, which can easily be
grouped together.
There are possibilities for strings too. Hashes, binary search etc.
Sure, but these are not so easy to optimize automatically.
--
Rudy Velthuis http://rvelthuis.de

Hoare's Law Of Large Programs: Inside every large program is a
small program struggling to get out.
Rudy Velthuis
2010-02-17 22:08:41 UTC
Permalink
Post by Maarten Wiltink
Post by Cooper
case mystring of
// Action
'dog' : out := 'cane';
'cat': out := 'gatto';
'fish': out := 'pesce';
else
// Error (do something)
end;
Doing so, delphi back me error telling me that required ordinal type.
Correct. It's simply a limitation of the case statement. You can for
if (MyString='dog')
then Out:='cane'
else if (MyString='cat')
then Out:='gatto'
else if (MyString='fish')
then Out:='pesce'
else Error;
Of course, it's not exactly pretty, and I'm not even talking about my
preferences for laying out iterated if-statements.
uses
StrUtils;

const
Texts: array[0..1, 0..2] of string = (
('dog', 'cat', 'fish'),
('cane', 'gatto', 'pesce')
);

begin
I := IndexText(mystring, Texts[0]);
if I >= 0 then
Result := Texts[1, I]
else
Result := '';
end;
--
Rudy Velthuis http://rvelthuis.de

"I wouldn't mind dying - it's the business of having to stay
dead that scares the shit out of me." -- R. Geis.
Rudy Velthuis
2010-02-17 21:55:11 UTC
Permalink
Post by Cooper
Hello, i have this small problem, me need check a string and to
second of content of string take a choose, so i have thinked to this
case mystring of
// Action
'dog' : out := 'cane';
'cat': out := 'gatto';
'fish': out := 'pesce';
else
// Error (do something)
end;
Doing so, delphi back me error telling me that required ordinal type.
And that is correct.
--
Rudy Velthuis http://rvelthuis.de

"The truth is more important than the facts."
-- Frank Lloyd Wright
BRoberts
2010-02-20 16:24:51 UTC
Permalink
Post by Cooper
Hello, i have this small problem, me need check a string and to second of
content of string take a choose, so i have thinked to this solution (just
case mystring of
// Action
'dog' : out := 'cane';
'cat': out := 'gatto';
'fish': out := 'pesce';
else
// Error (do something)
end;
But so i can solve this situation? Me have string value and not integer
value.
I like to use the TypInfo unit and an enumerated type:

Uses TypInfo;
. . .
Type tMyAnimals = (myDog, myCat, myFish);
. . .
case tMyAnimals (GetENumValue (TypeInfo (tMyAnimals), 'my' + myString)) of
myDog : out := 'cane';
myCat : out := 'gatto';
myFish : out := 'pesce';
end;
. . .

Better yet, IMO

Type tTranslateVector = array [tMyAnimals] of string;
. . .
Const tItalian : tTranslateVector = ('cane', 'gatto', 'pesce');
. . .
out := tItalian [tMyAnimals (GetENumValue (TypeInfo (tMyAnimals), 'my' +
myString))];

Loading...