Reputation: 17132
I need to iterate over a FOR loop with 2 variables. I searched for something like this:
DECLARE
counter INT;
max INT;
...
FOR counter IN REVERSE 1..10, max IN 1..5 LOOP
-- some code
END LOOP;
but couldn't find any reference. Is there such thing ?
I'm trying to reverse the order of the elements of an array:
DECLARE
TYPE IntArray IS VARRAY(80) OF INT; -- array of 80 ints
myArray IntArray;
counter INT; -- counter variable 1
max_limit INT; -- counter variable 2
tmp INT;
BEGIN
myArray := IntArray();
max_limit := 20;
myArray.EXTEND(max_limit);
-- I want to do something like this:
FOR counter IN 1..10, max_limit IN REVERSE 10..20 LOOP
-- reverse elements
END LOOP;
END;
Upvotes: 0
Views: 1575
Reputation: 922
From the syntax of for loop, I am not sure if you can have two index in single loop.
Upvotes: 2
Reputation: 191245
You want one loop, where one variable decreases while the other (loop counter) increases, at the same rate of one per loop (based on your comments). You don't need both variables as part of the loop itself (and can't do that anyway, as the for loop
syntax shows), but as they're related you can just calculate one from the other:
FOR counter IN 1..10 LOOP
tmp := max_value - counter;
-- reverse elements
END LOOP;
Or decrement directly:
FOR counter IN 1..10 LOOP
max_value := max_counter - 1;
-- reverse elements
END LOOP;
You haven't shown the array you want to reverse, and not sure why your two variables seem to be offset by 10 - may be you plan to append the reverse of the existing one onto the end of it, rather than replacing it? You don't really need a second variable in either case though.
Upvotes: 1