Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get key and value of associative arrays

My problem is that i want to get key and value from an assosiative array, but i only can find how to get the value from the key. This is what i find:

DECLARE
 TYPE assoc_array IS TABLE OF VARCHAR2(30)
 INDEX BY VARCHAR2(30);

 state_array assoc_array;
BEGIN
  state_array('Alaska') := 'Juneau';
  state_array('California') := 'Sacramento';

  dbms_output.put_line(state_array('Alaska'));
  dbms_output.put_line(state_array('California'));

END;

This prints Juneau and Sacramento, but i want something like this:

DECLARE
 TYPE assoc_array IS TABLE OF VARCHAR2(30)
 INDEX BY VARCHAR2(30);

 state_array assoc_array;
BEGIN
  state_array('Alaska') := 'Juneau';
  state_array('California') := 'Sacramento';

    for x in 1..state_array.count loop
    dbms_output.put_line(state_array(x).key || state_array(x).value);
    end loop;
END;

Is that possible?. Thanks in advance!!

like image 912
Aramillo Avatar asked Jan 09 '23 08:01

Aramillo


1 Answers

Actually there is a way, kindly consider the code bellow

declare
   type assoc_array is table of varchar2(30) index by varchar2(30);

   state_array assoc_array;
   l_idx varchar2(30);
begin
   state_array('Alaska') := 'Juneau';
   state_array('California') := 'Sacramento';

   l_idx := state_array.first;
   while (l_idx is not null) loop
      dbms_output.put_line('Key = ' || l_idx || ':Value = ' || state_array(l_idx));
      l_idx := state_array.next(l_idx);
   end loop;
end;

The output will be

Key = Alaska:Value = Juneau
Key = California:Value = Sacramento
like image 188
mikron Avatar answered Jan 17 '23 14:01

mikron