Thursday, March 8, 2012

Unforgotten elements

Nested tables differs from other collection types. They have useful facility. You can strike out one or more items from nested table regardless of their location in the collection. Nested table does not shrink collection and "holds" places which were deleted. But forget it. You can't use such spots like cells to place some values to it. You should also test collection with EXISTS method to sequential search after moving off at least one element.

Occasionally (very seldom may be) it would be useful to have API to obtain full information about nested table including lost elements. Here are simple example of such trivial API.



-- Define nested table type (certainly, type of elements may be differ)
CREATE TYPE t_nest_tab IS TABLE OF NUMBER;

DECLARE
    l_tab t_nest_tab;
BEGIN
    -- Populate nested table
    l_tab := t_nest_tab(1, 2, 3, 4, 5);

    -- Make it sparse
    l_tab.DELETE(2);

    show_tab(l_tab);
END;
show_tab() is the first procedure in my modest API. It prints statistics and map for nested table. Outcome is follow.

anonymous block completed
Volume :5
Real   :4
Deleted:1
**********
1:1
2:deleted
3:3
4:4
5:5
Next procedure is press_tab(). The procedure takes sparse nested table as formal parameter and makes it densed (press it). Suppose that we implement such snippet of code as follows:

DECLARE
    l_tab t_nest_tab;
BEGIN
    -- Populate nested table
    l_tab := t_nest_tab(1, 2, 3, 4, 5);

    -- Make it sparse
    l_tab.DELETE(2);

    show_tab(l_tab);

    -- Make it dense
    press_tab(l_tab);

    show_tab(l_tab);
END;
Here is outcome:
anonymous block completed
Volume :5
Real   :4
Deleted:1
**********
1:1
2:deleted
3:3
4:4
5:5
Volume :4
Real :4
Deleted:0
**********
1:1
2:3
3:4
4:5

The source code of the show_tab() and press_tab() procedures is available here.

No comments:

Post a Comment