Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

For loop with 2 iterator variables

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 ?

EDIT:

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

Answers (2)

Tony Bao
Tony Bao

Reputation: 922

From the syntax of for loop, I am not sure if you can have two index in single loop.

enter image description here

Upvotes: 2

Alex Poole
Alex Poole

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

Related Questions