Thursday, July 9, 2009

The Equal Sign and Double-Equal Sign

The use of equal sign and double-equal sign is really an interesting topic in programming.
In computer programming, the assignment operation and the equal-comparison operation are two distinct operations. How to 'represent' these two operations ? The equal sign(s) plays an very important role.
In some older programming languages such as BASIC & COBOL, single equal sign is used for both assignment and equality comparison. For example, here is a COBOL snippet:

  COMPUTE X = (5 + 1) * (5 - 1).   
  IF X = 24
  THEN DISPLAY 'YES, 24'
  ELSE DISPLAY 'NOT 24'
  END-IF.


Pascal, as an language for teaching programming, wants to tell the programmer the difference between the assignment operation and the equal-comparison operation. Pascal uses colon-equal (i.e. :=) as assignment operation and single-equal sign as equality comparison. Here is a Pascal snippet:

  x := (5 + 1) * (5 - 1) ; { Assignment uses := } 
  if( x = 24 )             { Equal comparison uses = }   
  then writeln('Yes, 24');
  else writeln('Not 24') ;


Distinguishing assignment and equal-comparison is of great merit in programming. Many modern programming languages agree with this theory. In C programming lanaguage, however, an alternate approach is used. She uses single-equal sign as assignment operator and double-equal signs as the equality comparison operator. C++ and Java also follow this approach. Here is a C snippet:

  x = (5 + 1) * (5 - 1) ;  /* Assignment uses = */
  if( x == 24 )            /* Equal comparison uses == */    
       printf('Yes, 24');
  else printf('Not 24') ;

Why choose single-equal sign (instead of colon-equal or double-equal or else) as assignment operator ? The answer is for efficiency. It was told that assignment operation is a type of very frequent operation. Using a single character (instead of two or more characters) can gain speed in writing programs. Programmers input fewer keystrokes.

Although efficiency is the advantage, this violate the normal behavior of human beings. Most human beings on Earth learns arithmetics and mathematics at his very early age. When learning arithmetics, the single-equal sign represents EQUAL. Some will learn mathematics for more than 10 years before learning programming. As a result, treating single-equal sign as EQUAL is nearly an intrinsic action of human being. Here is a very simple mathematical equation:

      x + 3 = 9        x plus 3 equals 9    
      x = 9 - 3        x equals 9 minus 3
      x = 6            x equals 6


Imaging that, in the middle of the night, you are woken up by an emergency pager call to debug this program snippet:


  x = (5 + 1) * (5 - 1) ;   
  if( x = 24 )      // The programmer intends for an equal comparsion   
       printf('Yes, 24');
  else printf('Not 24') ;
Even experienced programmer may not spot the bug instantly. This is because they intrinsically treat single-equal sign as EQUAL.

To solve this problem, some suggest to put the constant in the left hand side when doing equality comparison such as this snippet:

  x = (5 + 1) * (5 - 1) ;   
  if( 24 = x )     // The compiler will shouts    
       printf('Yes, 24');
  else printf('Not 24') ;
With this method, the compiler will certainly shouts at you. Then, the programmer can fix the bug as:

  x = (5 + 1) * (5 - 1) ;   
  if( 24 == x )
       printf('Yes, 24');
  else printf('Not 24') ;
But, reading "twenty-four equals x" sounds not so comfortable as reading "x equals twenty-four".

Also, this method cannot be used when comparing two variables like this one:
  x = 5 + 1 ;   
  y = 5 - 1 ;
  z = x     ;
  if( x = y )     // Some compiler will assume an assignment   
       printf('the same') ;   
  else printf('different');   
Even the compiler gives you warning, the programming may overlook, especially when there are thousands of warnings.

In my programming life, I have another method to tackle this problem. I use SPACE. Look at this equation:
      x + 3 = 9   
      x = 9 - 3
      x = 6
Do you notice that there is SPACE in both the LEFT and RIGHT hand side of the equal-sign ? Many human beings will do the same. So, my method is to put a space in BOTH the LHS and RHS when doing equality comparison. When doing assignment operation, I put space ONLY at the RHS of the assignment operator like this:

  x= 5 + 1 ;   
  y= 5 - 1 ;
  z= x     ;                // No space at LHS of =
  if( x == y )              // Spaces at both LHS and RHS of ==   
       printf('the same') ;
  else printf('different');

If you adopt this programming style and make it as a habit, you will feel very odd when seeing this:


  x= 5 + 1 ;
  y= 5 - 1 ;
  z= x     ;
  if( x = y )
       printf('the same') ;   
  else printf('different');
This is because putting spaces at both the left & right side of the SINGLE equal sign is NOT your programming habit. There must be something wrong. You can then get aware that you should use double-equal sign.

Alvin SIU
2009-07-09
Copyright/Licence Information:
All information and coding in this article is offered at no charge for NON-COMMERCIAL PERSONAL USE only.
This blog and the coding is copyright.
Reproduction of this blog and its coding in whole or in part in paper or digitally or in any other forms without the explicit written permission of the author is strictly prohibited.

Disclaimer:
All information in this article is distributed "as is" and is UNSUPPORTED.
NO WARRANTY of any kind is expressed or implied.
You use AT YOUR OWN RISK.
The author will not be liable for any data loss, damages, and loss of profits or any other kind of tangible or intangible loss while using or misusing wholly or partly of the information.

Friday, May 22, 2009

Undelete a PDS member

The following steps show how to un-delete a member of a PDS dataset in MVS
  1. Make sure the PDS dataset is NOT compressed
  2. Go to TSO Ready prompt
  3. Input pds
  4. Input the PDS dataset name
  5. Input restore
  6. answer Y to restore a member (answer N to skip)
    Noted that, for the same name, there happens to be more than one deleted copy.

Monday, May 4, 2009

Different Methods to Specify SAS Config File

There are different methods to config SAS in unix environment. First of all, for every installatoin, there is a config file stored in the installation directory alongside with the SAS executable file (i.e. the sasroot directory). This file is named config.sas in version-6, sasv8.cfg in version-8 and sasv9.cfg in version-9. This config file is effective for all users in this machine using this SAS. For example, this line in the config file can let all users to share the SAS working directory:
-work '/production/global/sas_working_directory' ;

However, if there is a config file in the $HOME directory e.g.
/home/alvin/config.sas
, SAS will use this $HOME config file instead.

Using this method, each programmer can has his/her own SAS configuration setting, e.g. using his own very large working directory instead of the global one :
-work '/my_large_filesystem/sas_working_directory' ;

But, when there are some other parameters changes e.g.
-sortsize 32M
and the program is rolled out to production, how can you ensure that the config file is also adjusted to be similar to the programmer's own config file (i.e. to -sortsize 32M) ? The change is ONLY in the programmer's config file in the programmer's $HOME directory. This is a serious drawback. (Actually, the change is not reflected in system test and user acceptance test either.)

Also, when upgrading from SAS version-6 to version-8, the name of the config file is changed from config.sas to sasv8.cfg. All user's config file under $HOME directory will be affected. Similarly when upgrading from version-8 to version-9.

Starting from version-8, we can also use environment variables SASV8_OPTIONS and SASV8_CONFIG to specify the config files. Using this method, it is more easy for all team members of a department to SHARE a common config file. For example:
export SASV8_OPTIONS='-config /company/department/common/sasv8.cfg' sas my_program.sas
or
export SASV8_CONFIG=/company/department/common/sasv8.cfg sas my_program.sas

These environmental variables can be specified in a common profile file. All team members has to call this departmental common profile file to make it effective for the login sessoin.

Another method is to specify the config file as command line option when calling the SAS executable. The advantage of using this method is the capability of specifing different configuration under different development phases. For example, in development phase, we can use a development configuration file like this :
export MYSASCONFIGFILE=/project/dvt/sasv8.cfg sas -config $MYSASCONFIGFILE my_program.sas
Then, during system test, we can do this :
export MYSASCONFIGFILE=/project/syt/sasv8.cfg sas -config $MYSASCONFIGFILE my_program.sas
During user acceptance test, we can do this :
export MYSASCONFIGFILE=/project/uat/sasv8.cfg sas -config $MYSASCONFIGFILE my_program.sas
Using this method, we can adjust the parameters (e.g. the working directory) in different development phases. In case some parameters needed to be adjusted, the UAT version of sasv8.cfg file can also be check-in / check-out to rollout to production.

By the way, since SAS version-8 and versoin-9 has some more complicated ways to specify the config file please refer here for the details for version-8 and this for version-9.

In short, before using SAS for any project, the first thing is to define the way of using the config file. It is better to use a systematic approach, rather than an ad-hoc way (e.g. the $HOME config file method).


Thursday, April 30, 2009

SAS Operator for Not-Equal

For many programmers, Pascal is the first programming language learnt. In Pascal, the operator <> means not equal.
SAS also has such a <> operator. But, this <> operator does NOT mean not-equal, it means MAX. It simply return the maximum of the 2 values under comparsion.
Actually, the offical not-equal operator in SAS are :
  1. ne
  2. ^=
  3. ¬=
  4. ~=
This is the link for SAS version-8 operators for your reference.
Frankly speaking, this quite odd usage of the <> operator in SAS will easily make some beginners fall into the trap of assuming <> to stand for not-equal. In SAS version-8, you can change the warning level to let the interpreter to issue a warning message when it sees a <> operator to alert the programmer that it is a MAX operator.
Since in real life the usage of getting the maximum using the <> operator is quite rare, my previous working experience is to use a pre-compiler alike construct to scan all SAS program to forbid using the <> operator. This can avoid falling into the trap of mistakenly using it to mean not-equal for everyone in the department. Actually, in SAS, there is a max() function which perform SIMILAR processing as the <> operator.

Substring in SAS

In many programming languages, there is a substr() function which will extract a portion of a string. SAS also has such a function. But, in SAS the substr() function is a little bit complicated.

In SAS version-8, if you put the substr() function on the left of the = sign, the substr() is actually a procedure to substitute string. It will replace a portion of the argument string with values at the right of the = sign. This link shows the version-8 syntax and here is the version-9 syntax.

If, however, the substr() function is put at the right of the = sign, the behavior of the substr() function is the normal sub-string function as expected by many ones. It will return a portion of the argument string. This link is the version-8 syntax and here is the version-9 syntax.

In SAS version-8, there is another function called kstrstrb(). Its function is to extract a substring in terms of byte. The DBCS version of this function is called ksubstr(). SAS version-9 also has these two functions.

In short, putting the substr() function on the LHS or RHS of the = sign will have very different behaviours. Putting it on the LHS will alter the argument string, just like passing parameter by reference. Putting it on the RHS is a function call with parameters passed by value and the argument string will NOT be altered.

By the way, when writing SAS macro, there are %substr() and %qsubstr() macro functions doing similar substring extraction processing.




Tuesday, April 14, 2009

Blackberry Signature Keys and Sign .cod File

This web page Install RIM Blackberry Signature Keys and Sign .cod File tells the necessary steps for handling the signature keys after receiving them.

I follow the steps to 'install' the 3 csi files as emailed from Blackberry. After this 'installation' the RBB, RRT and RCR keys are 'registered' with RIM.

Afterward, double-click the cod file. A window will be displayed to ask for signing the application using the RBB, RRT and RCR keys.

After signed, the .cod file is updated.

Throughout the whole process, there is nothing to do with Eclipse.




Thursday, April 9, 2009

My First Day Experience with Google Chrome 1.0.154.53

After installing Google Chrome 1.0.154.53, the first web page loaded is "Google Chrome: The application failed to initialize properly (0xc0000005)"

To solve this error, use Firefox to google search. This URL in techblissonline.com really helps. It told me to add a --no-sandbox option to open the application.

This is my first day experience with Google Chrome. The moral of this story is : there must be at least two browsers in your computer. In case one browser has error, you can use another one to search the internet for solution !


Duplicate Open Current Folder in a New Window

Sometimes after I opened a folder in Win7, I would like to duplicate open the same folder again in another explorer window. Then, I can ope...