The “Associative Arrays” are also known as “Index-By” tables in PL/SQL. We all know that a variable can hold only one value (as the variable occupies one memory location based on the data type chosen). An “Associative Array” can also be considered a single variable, but with more than one memory location.
An “Associative Array” can hold a huge amount of information in several memory locations, identified by some “index.” This “index” could simply be an integer or string, or something else.
In general, an “Associative Array” stores pairs of data (either sequentially or non- sequentially). Each pair of data would generally contain a “key” and a “value.” If stored sequentially, the “key” would be nothing but a consecutive number holding the “value.” While storing information into an “Associative Array,” the user needs to specify both “key” and “value.”
This concept looks very similar to the concept of arrays in C/C++. In C/C++ an array is a continuous memory of several locations starting with zero as index. But, here the “index” could be anything (there is no strict rule for working with the “index”).
Also, it is as much as same HashTable in C#.
Members of Associative Array
COUNT : It returns the number of elements available in the particular Array.
FIRST : It gives the first key available within the associative array.
LAST : It gives the last key available within the associative array.
NEXT(index): It returns the next key from the current key available in
variable “index”.
PRIOR(index): It returns the previous key from the current key
available in variable “index”.
DELETE(index): Deletes the key and associative value of particular key.
DELETE(start, end): Delete keys and values of specific range.
Example:
DECLARE
TYPE NUMBERARRAY IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
v_TYPENUMBER NUMBERARRAY;
v_TOTAL NUMBER := 0;
iCNT NUMBER;
BEGIN
v_TYPENUMBER(51) := 1200;
v_TYPENUMBER(2) := 200;
v_TYPENUMBER(30) := 600;
iCNT := v_TYPENUMBER.FIRST;
WHILE iCNT <= v_TYPENUMBER.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('Index : ' || iCNT);
v_TOTAL := v_TOTAL + v_TYPENUMBER(iCNT);
iCNT := v_TYPENUMBER.NEXT(iCNT);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Result = ' || v_TOTAL);
END;
The Output Will be like,
Index : 2
Index : 30
Index : 51
Result = 2000
No comments:
Post a Comment