From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 20 Apr 92 16:39:56 GMT From: cs.utexas.edu!ut-emx!hasan@uunet.uu.net (David A. Hasan) Subject: Re: loop indices Message-ID: <70628@ut-emx.uucp> List-Id: In a previous article, I asked for suggestions on how to deal with loop indices and gave the following example. > > DECLARE > TYPE Vector IS ARRAY( Positive RANGE <> ) OF Integer; > p1 : CONSTANT Positive := 21; > p2 : CONSTANT Positive := 51; > size : Natural := 5; > > v1 : Vector( p1 .. (p1+size-1) ); > v2 : Vector( p2 .. (p2+size-1) ); > BEGIN > FOR i IN 1..size LOOP > v2(i) := v1(i); -------------------(!) > END LOOP; > END; > This was an unfortunate example, for as numerous people suggested, slicing is the obvious solution. Short of that, there is some POSITIVE arithmetic which can be used to "play" with the indices of one array while the loop index is used to index into the other array. Variations on this approach were also suggested by several people. Thank you all for your comments. I guess in my zeal to simplify my problem, I posted an overly simple example. My real interest is in situations like the following. The important differences in this new example are 1) the two array variables of interest are of different types, 2) the type of the associated index are also different. DECLARE TYPE Index_1 IS (a,b,c,d,e,f,g); TYPE Array_1 IS ARRAY(Index_Type1 RANGE <>) OF Integer; TYPE Index_2 IS (aa,bb,cc,dd,ee,ff,gg); TYPE Array_2 IS ARRAY(Index_Type2 RANGE <>) OF Integer; v1 : Array_1( a..e ); v2 : Array_2( bb..ff ); i1 : Index_1; i2 : Index_2; BEGIN i1 := v1'FIRST; i2 := v2'FIRST; FOR i IN 1..5 LOOP v1(i1) := v2(i2); IF i<5 THEN i1 := Index_1'SUCC( i1 ); i2 := Index_2'SUCC( i2 ); END IF; END LOOP; END; In this case, slicing is not an option. Neither is it possible to perform arithmetic *directly* on the indices of either or (or both). Of course, using 'POS and 'VAL it is possible to come up with a solution which does not require the IF...END IF; in my example above. I prefer the code above for the following reasons: 1) the explicit use of 'SUCC makes it clear what's going on (there is no index arithmetic hiding my intent), 2) the two array objects, and , are dealt with "symmetrically" (i.e., neither is given the distinguished position of defining the LOOP index type), 3) the method applies equally to all ARRAY types with any sort of index type. I have no inherent opposition to using 'POS and 'VAL. I just "prefer" the appearance of the code in this example. So... My first question is this: does this code carry a performance hit with it (relative to analogous code which works with 'POS and 'VAL)? My second question is this: is there some other approach to solving this problem in a "readable" way? -- | David A. Hasan | hasan@emx.utexas.edu