home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / ARRAYDOC.ZIP / ARRAYS.TXT
Encoding:
Text File  |  1987-01-14  |  8.7 KB  |  266 lines

  1.   ARRAYS: SOME QUESTIONS AND ANSWERS
  2.  
  3.   by F. Ho; edited by Renee Gentry
  4.   =========================================================================
  5.  
  6.   The information below has been gathered from notes developed within the
  7.   Support Department.  They appear in Question/Answer format.
  8.   
  9.   ===========  
  10.   
  11.   Q:   - How is memory allocated for arrays?
  12.   A:   - Memory is allocated "dynamically"
  13.           
  14.               For example:
  15.               
  16.               If an array of 100 elements has been declared, a pointer
  17.               is set from the memvar area to a small chunk of memory area
  18.               where a reference to the array elements are kept. This 
  19.               array area is 22 bytes wide times the number of elements long.
  20.               (See figure 1)
  21.   
  22.               If the values in the array elements are of type NUMERIC,
  23.               they are stored within this area in their respective
  24.               storage element area. (See figure 2)
  25.   
  26.               If the values were of type CHARACTER, a pointer is set from 
  27.               the storage element area of that array to any contiguous
  28.               area of memory that can hold that string. (See figure 3)
  29.  
  30.  
  31.      figure 1:
  32.               memvar area
  33.                ------------------                 22 bytes
  34.               |                  |               <--long-->
  35.                ------------------               |-----------|
  36.               |                  |              |-----------|      
  37.                ------------------               |-----------|   Array(100)
  38.               |                  |              |-----------|      |
  39.                ------------------               |-----------|      |
  40.               |  Array(100)      | -----------> |-----------|      
  41.                ------------------               |-----------|
  42.               |                  |              |-----------|
  43.                                               |         |
  44.                  
  45.  
  46.  
  47.      figure 2:
  48.  
  49.  
  50.                    22 bytes long
  51.                 ------------------  
  52.                |  1)              |            Array(2) = 75
  53.                 ------------------             Array(3) = 40
  54.               |  2)  75          |
  55.      |          ------------------ 
  56.   Array(100)   |  3)  40          |
  57.      |          ------------------ 
  58.      |         |  4)              |
  59.                ------------------ 
  60.                |                  |
  61.                                   
  62.                   
  63.  
  64.  
  65.      figure 3:
  66.  
  67.                                                         ---------------
  68.                    22 bytes long                 ---> | "Hello There" |
  69.                 ------------------              |       ---------------
  70.                |  1)              | ------------
  71.                 ------------------   
  72.               |  2)  75          |      
  73.      |          ------------------               Array(1) = "Hello There"
  74.   Array(100)   |  3)  40          |              Array(4) = "Waz Happenin..."
  75.      |          ------------------ 
  76.      |         |  4)              | -----
  77.                ------------------       |
  78.                |                  |      |                -------------------
  79.                                         ------------> | "Waz Happenin..." |
  80.                                                           -------------------
  81.  
  82.  
  83.   ==========
  84.   
  85.   Q:   - How do arrays respond to the TYPE() function?
  86.   
  87.   A:   - i  - a TYPE() done on an array name will return "A" for "Array"
  88.             e.g.
  89.                  - declare ARRAY[10]
  90.                  - ? type("ARRAY")             - * will return "A"
  91.   
  92.   
  93.  
  94.        - ii - a TYPE() done on an array element will return "U" for 
  95.             "Undefined" - regardless of any values assigned to it
  96.             e.g.
  97.                  - ARRAY[1] = 25
  98.                  - ARRAY[2] = "Hello"
  99.                  - ? type("ARRAY[1]")          - * will return "U"
  100.                  - ? type("ARRAY[2]")          - * will return "U"
  101.  
  102.  
  103.        - iii- on the other hand, if the array elements are assigned to
  104.               variables, a TYPE() function done on the respective variable
  105.               will return the appropriate type:
  106.             e.g.
  107.                  - ARRAY[1] = 25
  108.                  - ARRAY[2] = "Hello"
  109.                  - A = ARRAY[1] 
  110.                  - B = ARRAY[2]
  111.                  - ? type("A")                 - * will return "N" 
  112.                  - ? type("B")                 - * will return "C"
  113.  
  114.   ==========
  115.   
  116.   Q:  - How would you collapse a declared array?
  117.   A:  - Like all other variables, you can:
  118.     
  119.         1 - RELEASE the array   e.g. release ARRAY
  120.         2 - assign it a false value e.g. ARRAY = .F.
  121.        *3 - if the array was declared in a lower procedure,
  122.             a return to the higher calling procedure destroys
  123.             out the array
  124.  
  125.   ===========
  126.  
  127.   Q:   - How would you create a 2-dimensional array in Clipper?
  128.   
  129.   A:   - First, a background note on how arrays are actually "declared"
  130.          or "dimensioned" by any computer languages:
  131.             - when one issues the commands:
  132.                            
  133.                         DIM ARRAY(10)       - in BASIC
  134.                         DECLARE ARRAY[10]   - in CLIPPER
  135.   
  136.             - a single array dimension of 10 elements is allocated.
  137.  
  138.  
  139.  
  140.             - Or when one issues the command:
  141.  
  142.                          DIM ARRAY(3,5) - in BASIC for a 
  143.                                                two-dimensional array
  144.  
  145.             - a SINGLE array of 15 (3*5) elements is allocated.
  146.               The only thing that makes it work like it's two-
  147.               dimensional is a built-in function in BASIC that
  148.               does the proper POSITIONAL-ALLOCATION for the 
  149.               element concerned.
  150.  
  151.               This "position-allocator" function is what's missing
  152.               in Clipper.
  153.               But, you can get around this by making your own
  154.               UDF which accomplishes the same thing.
  155.  
  156.  
  157.  
  158.     Below is an example on how to declare a 2-dimensional array:
  159.  
  160.      ----------------------------------------------------------------------
  161.           clear
  162.           declare ARRAY[3*5]       && or declare ARRAY[15]
  163.  
  164.           ARRAY[DIM2(2,3)] = "TEST"     && DIM2 is our pos-alloc function
  165.  
  166.           ? ARRAY[DIM2(2,3)]            && should print "TEST"
  167.           quit
  168.  
  169.  
  170.           function DIM2                 && our position-allocator function
  171.           parameters X,Y
  172.           return(((X - 1) * 5) + Y)
  173.  
  174.      ----------------------------------------------------------------------
  175.  
  176.      The above can be generalised as:
  177.  
  178.           clear
  179.           ITEMS = 3
  180.           ELEMENTS = 5
  181.           declare ARRAY[ITEMS * ELEMENTS]
  182.  
  183.           ARRAY[DIM2(2,3)] = "TEST"
  184.  
  185.           ? ARRAY[DIM2(2,3)]
  186.           quit
  187.  
  188.           function DIM2
  189.           parameters X,Y
  190.           return(((X - 1) * ELEMENTS) + Y)
  191.  
  192.      -----------
  193.  
  194.           - the above declaration will set up a single dimensional array
  195.             of 15 elements:
  196.  
  197.                ARRAY  01
  198.                       02
  199.                       -
  200.                       -
  201.                       -
  202.                       14
  203.                       15
  204.  
  205.  
  206.           - but the POS-ALOC function can make it access the table as if
  207.             it were:
  208.  
  209.                           1    2    3
  210.                          --------------
  211.                ARRAY 1  | 01 | 06 | 11 |
  212.                          --------------
  213.                      2  | 02 | 07 | 12 |
  214.                          --------------
  215.                      3  | 03 | 08 | 13 |
  216.                          --------------
  217.                      4  | 04 | 09 | 14 |
  218.                          --------------
  219.                      5  | 05 | 10 | 15 |
  220.                          --------------
  221.  
  222.           - so that, the command:
  223.  
  224.                     ARRAY[DIM2(2,3)] = "TEST"
  225.  
  226.             - will store "TEST" at location:
  227.  
  228.                     ARRAY[DIM2(2,3)]
  229.  
  230.                          or
  231.  
  232.                     ARRAY[(((2-1) * 5) + 3)]
  233.  
  234.                          or
  235.  
  236.                     ARRAY[8]
  237.  
  238.  
  239.   ==========
  240.  
  241.   Note:
  242.   
  243.      - You CANNOT do a GET into an array element. Doing so will return a runtime
  244.        error message of "Type conflict in subscript" ,or it will just go right
  245.        through the read.
  246.  
  247.                @ 12,12 get ARRAY[1]        && invalid
  248.                read
  249.  
  250.  
  251.      - workaround:
  252.  
  253.                XX = ARRAY[1]
  254.                @ 12,12 get XX
  255.                read
  256.  
  257.  
  258.        or, at least initialize the array element you want to do a GET on:
  259.  
  260.                ARRAY[1] = space(10)
  261.                @ 12,12 get ARRAY[1]
  262.                read
  263.  
  264.   ==========
  265.   * eof *
  266.