% '------------------------------------------------------------------------------- 'File : dBase_Knowledge.asp 'Author : Surya Kumar 'Task : Knowledge Page for dBASE 'Version : 1.0.0.pk 10.08.2004 Base version 'Copyright : Ebase Interactive Inc., NY, USA '------------------------------------------------------------------------------- %>
Did You Mean "Exactly Equal" or "Begins With"?by Jim Sare In most other programming languages, a program snippet such as this: a = "JIM" b = "JI" if a = b // Do something endIf would mean if the value a is exactly equal to the value b, then do something. In dB2K and dBASE (since dBASE For Windows 5.0), this is not necessarily the case. Starting with dBASE For Windows 5.0 and later (including dB2K), a new operator == was implemented. Understanding the difference between the = and == operators becomes important. Also, the SET EXACT environment setting affects the comparison of character strings, and changes the meaning of the = comparison operator. If EXACT is on, the = operator means "exactly equal". If EXACT is off, the = operator means "begins with". However, the == operator always means 'exactly equal' independent of the EXACT setting. To illustrate: SET EXACT OFF ? "JIM" = "JI" // Displays true - JIM begins with JI ? "JI" = "JIM" // Displays false - JI does not begin with JIM ? "JIM" == "JI" // Displays false - JIM is not exactly equal to JI ? "JI" == "JIM" // Displays false - JI is not exactly equal to JIM SET EXACT ON ? "JIM" = "JI" // Displays false - JIM is not exactly equal to JI ? "JI" = "JIM" // Displays false - JI does not begin with JIM ? "JIM" == "JI" // Displays false - JIM is not exactly equal to JI ? "JI" == "JIM" // Displays false - JI is not exactly equal to JIM The dual meaning of the = operator based on the EXACT setting can be very powerful and useful if understood and used correctly. It is quite common to use the 'begins with' meaning of the = operator in applications. And with the newer == operator, exact comparisons can still be performed when EXACT is OFF. For this reason, whether you typically have EXACT ON or OFF, if an 'exactly equal' comparison is required, the comparison should be done using the == operator and not the = operator. The code will be more self-documenting, and will be more portable when sharing the same code with other users running dB2K or dBASE on other systems. Your code will operate consistently regardless of the setting of EXACT on other systems. In fact, the default EXACT setting is OFF when dB2K or dBASE is first installed. And it is recommended in the dB2K Online Help that you leave EXACT set OFF and use the == operator when exact comparison of character strings is required. This allows for immediate access to both types of comparisons using the = operator for 'begins with', and the == operator for 'exactly equal'. For further information regarding =, ==, and SET EXACT, see the dB2K Online Help for those topics and related topics. |