Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through an Associative Array 1

Status
Not open for further replies.

getjbb

MIS
Jun 4, 2003
139
US
If I have an associative array as follows:

bookkeeping(EXAM1) := 'Inventory';
bookkeeping(EXAM2) := 'Accounts receivable';
bookkeeping(EXAM3) := 'Inventory';
bookkeeping(EXAM4) := 'Property, plant and equipment';
bookkeeping(EXAM5) := 'Assets';
bookkeeping(EXAM6) := 'Accounts receivable';
bookkeeping(EXAM7) := 'Liabilities;

Is there a way to count the values by looping through the associative array keys?

I want to do something like:

Inventory 2
Accounts receivable 2
Property, plant and equipment 1
Assets 1
Liabilities 1

getjbb
 



Try this:
Code:
DECLARE
   TYPE txt IS TABLE OF VARCHAR2 (100)
      INDEX BY VARCHAR2 (100);

   bookkeeping   txt;

   TYPE num IS TABLE OF NUMBER
      INDEX BY VARCHAR2 (100);

   bkcount       num;

   t             VARCHAR2 (100);
   tx            VARCHAR2 (100);
BEGIN
   bookkeeping ('exam1') := 'Inventory';
   bookkeeping ('exam2') := 'Accounts receivable';
   bookkeeping ('exam3') := 'Inventory';
   bookkeeping ('exam4') := 'Property, plant and equipment';
   bookkeeping ('exam5') := 'Assets';
   bookkeeping ('exam6') := 'Accounts receivable';
   bookkeeping ('exam7') := 'Liabilities';

   t := bookkeeping.FIRST;

   WHILE t IS NOT NULL
   LOOP
      tx := bookkeeping (t);
      DBMS_OUTPUT.put_line (t || '= ' || tx);

      IF bkcount.EXISTS (tx)
      THEN
         bkcount (tx) := bkcount (tx) + 1;
      ELSE
         bkcount (bookkeeping (t)) := 1;
      END IF;

      t := bookkeeping.NEXT (t);
   END LOOP;

   tx := bkcount.FIRST;

   WHILE tx IS NOT NULL
   LOOP
      DBMS_OUTPUT.put_line (tx || '= ' || bkcount (tx));
      tx := bkcount.NEXT(tx);
   END LOOP;
END;
/
[3eyes]

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Thank you so much, LKBrwnDBA. This helps a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top