Associative Array Example

DECLARE
–– Define a varray of twelve strings.
TYPE months_varray IS VARRAY(12) OF STRING(9 CHAR);
–– Define an associative array of strings.
TYPE calendar_table IS TABLE OF VARCHAR2(9 CHAR) INDEX BY BINARY_INTEGER;
–– Declare and construct a varray.
month MONTHS_VARRAY :=
months_varray(‘January’,’February’,’March’,’April’,’May’,’June’
,’July’,’August’,’September’,’October’,’November’,’December’);
–– Declare an associative array variable.
calendar CALENDAR_TABLE;
BEGIN
–– Check if calendar has no elements, then add months.
IF calendar.COUNT = 0 THEN
DBMS_OUTPUT.PUT_LINE(‘Assignment loop:’);
DBMS_OUTPUT.PUT_LINE(‘–——––––––––’);
FOR i IN month.FIRST..month.LAST LOOP
calendar(i) := ”;
DBMS_OUTPUT.PUT_LINE(‘Index [‘||i||’] is [‘||calendar(i)||’]’);
calendar(i) := month(i);

END LOOP;
–– Print assigned element values.
DBMS_OUTPUT.PUT(CHR(10));
DBMS_OUTPUT.PUT_LINE(‘Post-assignment loop:’);
DBMS_OUTPUT.PUT_LINE(‘––––––––––———–‘);
FOR i IN calendar.FIRST..calendar.LAST LOOP
DBMS_OUTPUT.PUT_LINE(‘Index [‘||i||’] is [‘||calendar(i)||’]’);
END LOOP;
END IF;
END;

/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s