Board index » delphi » Sets in Delphi - is [apple] member of set [apple,pear]

Sets in Delphi - is [apple] member of set [apple,pear]

Quote
timothy.wil...@uni.massey.ac.nz wrote:
>Hi all
>I have a small problem with sets.  After I create a set like this
>type
>  Fruit = (Apple, Pear, Orange);
>  FruitSet = set of fruit;
>var
>  F : FruitSet;
>procedure blah;
>var
>  T:FruitSet;
>begin
>  T := [apple,pear];
>  ..
>end;
>How do I find out if [apple] is a member of the set???  I believe the
>keyword "in" is used, but I am not sure how.  I thought that "if [apple]
>in T then...." might work, but it doesn't.
>Thanks for any help or suggestions you may have.

Hi Timothy,

You need to say
   if (apple in T) then {Stuff}

You don't need the [] around apple.  The in operator works on a single
set member.  If you want, you can use the other set operators:

+       Union   compatible set types
-       Difference      compatible set types
*       Intersection    compatible set types

So you could say

if ([apple] * T) = [] then {apple is not in T}, but the IN operator is
much easier.

Jay Cole

===========
NitroSort provides mainframe file sorting performance at PC prices.
See www.nitrosort.com

 

Re:Sets in Delphi - is [apple] member of set [apple,pear]


Hi all

I have a small problem with sets.  After I create a set like this

type
  Fruit = (Apple, Pear, Orange);
  FruitSet = set of fruit;

var
  F : FruitSet;

procedure blah;
var
  T:FruitSet;
begin
  T := [apple,pear];
  ..

end;

How do I find out if [apple] is a member of the set???  I believe the
keyword "in" is used, but I am not sure how.  I thought that "if [apple]
in T then...." might work, but it doesn't.

Thanks for any help or suggestions you may have.

Cheers

Timothy Wild
Massey University
New Zealand

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

Re:Sets in Delphi - is [apple] member of set [apple,pear]


Quote
timothy.wil...@uni.massey.ac.nz wrote:
>How do I find out if [apple] is a member of the set???  I believe the
>keyword "in" is used, but I am not sure how.  I thought that "if [apple]
>in T then...." might work, but it doesn't.

  if Apple in T then
    // whatever

When you enclosed Apple in the braces, that made it a set instead of an
individual value.  The in operator only checks for individual occurring in
a set.  The intersection operator '*' can be used to find out if more than
one value occurs in a set like:

  AFruitSet := [Apple, Orange, Pear, Grape];
  AnotherSet := [Apple, Orange];
  if (AnotherSet * AFruitSet) = AnotherSet then
    // all items in AnotherSet were in AFruitSet
  else
    // Not all items occurred in both sets, but some items may be in both.

This might give you a better idea of how intersection works:

  AFruitSet := [Apple, Orange, Pear, Grape];
  AnotherSet := [Apple, Pineapple, Orange];
  IntersectSet := AFruitSet * AnotherSet;  
  // IntersectSet now contains [Apple, Orange]

Regards,
Brad
bstow...@pobox.com
Free Delphi Stuff:  http://www.pobox.com/~bstowers/delphi/

Re:Sets in Delphi - is [apple] member of set [apple,pear]


timothy.wil...@uni.massey.ac.nz wrote (incidentally):

Quote
>type
>  Fruit = (Apple, Pear, Orange);
>  FruitSet = set of fruit;

>var
>  F : FruitSet;

One of the problems in Borland Pascal is that the compiler can't
distinguish between Apple the fruit and Apple the ex-computer, so it is
usually ugly but smart to qualify it like:

Fruit_enum = (Fruit_apple, ... )
History_enum = (History_apple, History_TRS80, ...  )

Sorry, couldn't resist!

Andrew

Re:Sets in Delphi - is [apple] member of set [apple,pear]


Quote
Brad Stowers wrote:
>   AFruitSet := [Apple, Orange, Pear, Grape];
>   AnotherSet := [Apple, Orange];
>   if (AnotherSet * AFruitSet) = AnotherSet then
>     // all items in AnotherSet were in AFruitSet
>   else
>     // Not all items occurred in both sets, but some items may be in both.

Or possibly:

  if AnotherSet <= AFruitSet then
    // AnotherSet is a subset of AFruitSet
  else
 ...

Chris.

Other Threads