DECLARE
TYPE numlist IS TABLE OF NUMBER;
n NUMLIST := Numlist(1,2,3,5,7,11);
PROCEDURE print_numlist
(the_list NUMLIST)
IS
output VARCHAR2(128);
BEGIN
IF n.COUNT = 0 THEN
dbms_output.Put_line('No elements in collection.');
ELSE
FOR i IN the_list.FIRST.. the_list.LAST LOOP
output := output
||Nvl(To_char(The_list(i)),'NULL')
||' ';
END LOOP;
dbms_output.Put_line(output);
END IF;
END;
BEGIN
Print_numlist(n);
n.TRIM(2); -- Remove last 2 elements.
Print_numlist(n);
n.TRIM; -- Remove last element.
Print_numlist(n);
n.TRIM(n.COUNT); -- Remove all remaining elements.
Print_numlist(n);
-- If too many elements are specified,
-- TRIM raises the exception SUBSCRIPT_BEYOND_COUNT.
BEGIN
n := Numlist(1,2,3);
n.TRIM(100);
EXCEPTION
WHEN subscript_beyond_count THEN
dbms_output.Put_line('I guess there weren''t 100 elements that could be
trimmed.');
END;
-- When elements are removed by DELETE, placeholders are left behind. TRIM counts
-- these placeholders as it removes elements from the end.
n := Numlist(1,2,3,4);
n.DELETE(3); -- delete element 3
-- At this point, n contains elements (1,2,4).
-- TRIMming the last 2 elements removes the 4 and the placeholder, not 4 and 2.
n.TRIM(2);
Print_numlist(n);
END;
/
Using TRIM to Decrease the Size of a Collection
0 comments:
Post a Comment