Saturday, November 3, 2012

Ordlist() Function for Harbour, an Xbase++ function


There are functions in other xBase languages that are important, but are not in Harbour. One is Xbase++'s OrdList(). What's the work of OrdList()? Well, it lists the tag names of an index file. For example:

USE MySales.DBF
INDEX ON NAME         TAG NAMEX  TO SALES.CDX
INDEX ON ACCOUNT TAG ACCTX   TO SALES.CDX
...code
Running OrdList() we will get an array of tag names, which using the above example, will result in { NAMEX, ACCTX }.

Here's Xbase++'s documentation/example regarding OrdList():

 // OrdList()
// In the example, two index files for a customer file are created
// and then closed. The effect of the function OrdList() is
// shown.

   PROCEDURE Main
       USE Customer NEW EXCLUSIVE
      INDEX ON CUSTNO ;
           TAG C_Number ;
            TO CustA
       INDEX ON Upper(LASTNAME+FIRSTNAME) ;
           TAG C_LastName ;
            TO CustB
       SET INDEX TO CustA, CustB
       ? OrdList()          // Result: {C_NUMBER, C_LASTNAME}
       CLOSE Customer
    RETURN

I want the missing OrdList() in Harbour to be simulated, and the following is the code. (I limited my OrdName() up to 6 only, as it is the maximum based on my experiment. Our mentors out there know better. We may ask them through the Harbour Users Forum


FUNCTION MY_ORDLIST()
   LOCAL b,a:={},i
   SELECT ( Alias() )
   b:={OrdName(1),OrdName(2),OrdName(3),OrdName(4),OrdName(5),OrdName(6)}
   FOR i:=1 TO LEN( b )
       IF b[i] <> NIL
          aAdd( a, b[ i ] )
       ENDIF
   NEXT
RETURN a

May God bless our Harbour Family!