为什么使用 FIRST 和 LAST 方法不能解析由字符串索引的关联数组的所有内容

Why using FIRST and LAST method dosen't parse all the content of an associative array indexed by string

我有一个由包含 12 个元素的字符串索引的关联数组。 但是使用 FIRST 和 LAST 方法并不能解析存储在其中的所有元素。

declare
  TYPE tabperson IS TABLE OF varchar2(8) INDEX BY varchar2 (32000);
  wtabperson tabperson ;
begin
  FOR i IN 1..12
    LOOP
        wtabperson(i) := 'A' || i;
    end loop;

  dbms_output.PUT_LINE('content of index 11: ' || wtabperson('11'));
  dbms_output.PUT_LINE('content of index 12: ' || wtabperson('12'));
  dbms_output.PUT_LINE('size of the associative array: ' || wtabperson.COUNT);

  dbms_output.PUT_LINE(' Now I would like to parse all the content of the associative array 
   ');
  for i in wtabperson.FIRST.. wtabperson.LAST
    LOOP
        dbms_output.PUT_LINE('index ' || i || ' -------------->  ' || wtabperson(i));
    end loop;
end;

这是我在控制台中得到的:

[2021-07-25 19:05:33] content of index 11: A11
[2021-07-25 19:05:33] content of index 12: A12
[2021-07-25 19:05:33] size of the associative array: 12
[2021-07-25 19:05:33]  Now I would like to parse all the content of the associative array 
[2021-07-25 19:05:33] index 1 -------------->  A1
[2021-07-25 19:05:33] index 2 -------------->  A2
[2021-07-25 19:05:33] index 3 -------------->  A3
[2021-07-25 19:05:33] index 4 -------------->  A4
[2021-07-25 19:05:33] index 5 -------------->  A5
[2021-07-25 19:05:33] index 6 -------------->  A6
[2021-07-25 19:05:33] index 7 -------------->  A7
[2021-07-25 19:05:33] index 8 -------------->  A8
[2021-07-25 19:05:33] index 9 -------------->  A9

所以由字符串索引的关联数组包含12个元素: 索引值按以下顺序排列:'1'、'10'、'11'、'12'、'2'、'3'、'4'、'5'、'6'、'7'、'8 ','9'.

wtabperson.LAST 等于 '9' 而 '11' 低于 wtabperson.LAST 所以为什么我没有得到 wtabperson 的值('11') 当我使用 FIRST 和 LAST 方法解析数组时 ?

是否使用 FIRST 和 LAST 方法仅解析数组的一部分(如果是通过字符串索引的关联数组)?

为什么使用 FIRST 和 LAST 方法不能解析由 string 索引的关联数组的所有内容? [编辑] 在这个 post 中,问题是为什么 wtabpeson.LAST 在 '9' 阻塞,我得到了回应。

现在的问题是: 由于索引值 '11' 低于 '9',那么为什么 '11' 不在 wtabperson.FIRST 和 wtabperson.LAST 之间? wtabperson('11') 的值为 A11。那么为什么我在使用 FIRST 和 LAST 解析数组时没有得到索引“11”的值?

谢谢

, the documentation says 中所述:

For an associative array indexed by PLS_INTEGER, the first and last elements are those with the smallest and largest indexes, respectively. For an associative array indexed by string, the first and last elements are those with the lowest and highest key values, respectively.

其中 'highest' 和 'lowest' 基于 string comparison

对于您的数据,最低值为“1”,最高值为“9”。

所以当你这样做时:

for i in wtabperson.FIRST.. wtabperson.LAST

你基本上是在用你的数据做的:

for i in '1'..'9'

循环遍历这两个字符串之间的字符串值,因此您得到索引键列表“1”、“2”、“3”、“4”、“5”、“6”、“ 7'、'8'、'9'。

但它只是查看定义范围的高值和低值。 not 所做的是在 first 和 last 之间循环实际索引值。除了在循环声明中获取 first/last 之外,循环不查看集合。您(我认为)期望它获得索引键“1”、“10”、“11”、“12”、“2”……但它不是这样工作的。循环控制机制不知道“10”、“11”和“12”的存在——甚至不知道数组中存在“2”;只是它是'1'之后的下一个字符串。

如果你想遍历所有的索引值,你可以这样做:

declare
  ...
  idx varchar2(8); -- to match your key size
begin
  ...
  idx := wtabperson.FIRST; -- get first element
  while idx is not null
    loop
      dbms_output.PUT_LINE('index ' || idx || ' -------------->  ' || wtabperson(idx));
      idx := wtabperson.NEXT(idx); -- get next element
    end loop;
  END LOOP;

这会一直循环下去,直到索引值按排序顺序用完;根本不需要参考 last