home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / pascal / 6526 < prev    next >
Encoding:
Text File  |  1992-11-12  |  2.9 KB  |  100 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!mcsun!sun4nl!star.cs.vu.nl!sbakker
  3. From: sbakker@cs.vu.nl (Bakker S)
  4. Subject: Re: Comparing record types
  5. Message-ID: <BxLM1t.841@cs.vu.nl>
  6. Sender: news@cs.vu.nl
  7. Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
  8. References: <92316.095144F0O@psuvm.psu.edu>
  9. Date: Thu, 12 Nov 1992 10:20:17 GMT
  10. Lines: 88
  11.  
  12. <F0O@psuvm.psu.edu> writes:
  13.  
  14. >Hi!
  15.  
  16. >   Take a look at the following example program:
  17.  
  18.  
  19. >Type
  20. >TPoint = Record
  21. >   X, Y: Integer;
  22. >End;
  23.  
  24. >Var
  25. >   P1, P2: TPoint;
  26.  
  27.  
  28. >Begin
  29. >   P1.X := 10; P1.Y := 20;
  30. >   P2 := P1;
  31. >   If (P1 = P2) then Writeln('Equal');     { This line is incorrect }
  32. >End.
  33.  
  34. >   When I run the program, I get the message, 'Operand types do not
  35. >match operator'.
  36.  
  37. That's right. '=' only applies to atomic types (scalar, floating point, 
  38. string, set).
  39.  
  40. >   P1 and P2 are two variables of the same record type.  Now you can
  41. >directly assign one record to another, as long as they are the same type.
  42. >If there some simple way to compare P1 and P2 to see if they are equal,
  43. >without having to check each field for equality in my code?
  44. >   I was hoping I could say If (P1 = P2) then ..., but this does not
  45. >work.  Does anyone know of some trick so I can easily compare records?
  46.  
  47. The problem is, the compiler cannot tell on which fields to compare the 
  48. records. Should all fields match? Should only one field match (`primary key')?
  49. For this reason you cannot simply say `If (P1 = P2) then ...' or use
  50. any other relational operator for that matter.
  51.  
  52. What you might do is what C does with its strcmp(): write a PointCmp function.
  53.  
  54.     For simple comparison (`equal or not?'):
  55.  
  56.     function    PointCmp( P1, P2 : TPoint ) : Boolean;
  57.     begin
  58.         with P1 do
  59.         PointCmp := ( X = P2.X ) and ( Y = P2.Y );
  60.     end;
  61.  
  62.     Now your line would become:
  63.     >   If PointCmp(P1, P2) then Writeln('Equal');
  64.  
  65.  
  66.     For full comparison ( <, =, > ):
  67.     
  68.     function    PointCmp( P1, P2 : TPoint ) : Integer;
  69.     begin
  70.         with P1 do
  71.         if X < P2.X then
  72.             PointCmp := -1
  73.         else if X > P2.X then
  74.             PointCmp := 1
  75.         else if Y < P2.Y then
  76.             PointCmp := -1
  77.         else if Y > P2.Y then
  78.             PointCmp := 1
  79.         else PointCmp := 0;
  80.     end;
  81.  
  82.     Now your line would become:
  83.     >   If PointCmp(P1, P2) = 0 then Writeln('Equal');
  84.     And you could write:
  85.         If PointCmp(P1, P2) > 0 then Writeln('Greater');
  86.         If PointCmp(P1, P2) < 0 then Writeln('Smaller');
  87.  
  88.                 Hope this helps,
  89.                 Steven.
  90.  
  91. [=============================================================================]
  92. | Steven Bakker, Vrije Universiteit Amsterdam.                      |
  93. | sbakker@cs.vu.nl                                             |
  94. [-----------------------------------------------------------------------------]
  95. | ".. if a tree falls in the woods, and there's no one there to hear it, does |
  96. | it fall upwards or downwards. And its corollary, if a deaf man falls in the |
  97. | woods, does he make a sound?"                              |
  98. |                    -- Captain Rick.              |
  99. [=============================================================================]
  100.