Focal Point
[SOLVED] Assigning parameter values based on an array?

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/3937062126

June 14, 2012, 05:39 AM
mark66
[SOLVED] Assigning parameter values based on an array?
Hi,

I want to pass an array of values as a single parameter to my program. Something simple like “3,7,12,56,42”.

I want to loop through the array and for every element assign each one to a new parameter, named &P1, &P2 etc up to a max of 10.

Is there a command that I can use that will deconstruct the array if I tell it what the separator is?

Here is some pseudo code giving an idea of what I am thinking:

-SET &ARRAY = “3,7,12,56,42”;

-SET &COUNTER = 1;
-REPEAT ARRAY_LOOP 10 TIMES

-SET &P.&COUNTER = IF &ARRAY.&COUNTER NE ‘’ THEN &ARRAY.&COUNTER ELSE ‘’;

-SET &COUNTER = &COUNTER + 1;
-ARRAY_LOOP

-TYPE &P1 (=3)
-TYPE &P2 (=7)
-TYPE &P3 (=12)
...etc


Thanks

Mark

This message has been edited. Last edited by: mark66,


WebFocus 765. iSeries v5r4
June 14, 2012, 06:51 AM
<JG>
GETTOK
June 14, 2012, 07:57 AM
mark66
Thanks!

Had a fiddle, any ideas why I am only seeing the first character of my elements?

-DEFAULT &P1 = ' ';
-DEFAULT &P2 = '';
-DEFAULT &P3 = '';
-DEFAULT &P4 = '';
-DEFAULT &P5 = '';

-SET &ARRAY = 'AA,BB,CC,DD,EE';
-SET &COUNTER = 1;

-REPEAT ARRAY_LOOP 5 TIMES

-SET &P.&COUNTER = GETTOK(&ARRAY, 20, &COUNTER, ',', 2, &P.&COUNTER);
-TYPE &P.&COUNTER

-SET &COUNTER = &COUNTER + 1; 
-ARRAY_LOOP



WebFocus 765. iSeries v5r4
June 14, 2012, 08:07 AM
<JG>
quote:
-SET &P.&COUNTER = GETTOK(&ARRAY, 20, &COUNTER, ',', 2, &P.&COUNTER);


That should not work at all.

Try

-SET &P.&COUNTER = GETTOK('&ARRAY', 20, &COUNTER, ',', 2, 'A2');
June 14, 2012, 08:43 AM
j.gross
@mark66:

quote:
-DEFAULT &P1 = ' ';
-DEFAULT &P2 = '';
-DEFAULT &P3 = '';
-DEFAULT &P4 = '';
-DEFAULT &P5 = '';


In -SET, when you supply an amper variable as the final arg of a function, the length of its current value is used, and the output value will be adjusted, Procrusteanly (padded or truncated), to fit. Since you initialized the 5 receiving variables to '' or ' ', their length was set to 1, so GETTOK acted as if you explicitly coded a format of A1 as the final arg, and truncated its result accordingly.

(The minimum length of an amper var's value is 1, so '' has the same effect as ' ')

Bump the length of their default values and it works:
-DEFAULT &P1='  ', &P2='  ', &P3='  ', &P4='  ', &P5='  '

-SET &ARRAY = 'A,BB,CCC, D,EE';

-REPEAT ARRAY_LOOP FOR &P FROM 1 TO 5;

-SET &P.&P = GETTOK(&ARRAY, 20, &P, ',', 2, &P.&P);
-TYPE &P|: (&P.&P)
-ARRAY_LOOP
Output:
 1: (A )
 2: (BB)
 3: (CC)
 4: ( D)
 5: (EE)


@JG: Nope, if you quote the name of an amper var like that (in the RHS of a -SET), the name will be interpretted literally, & and all, and you wind up with
 1: (&A)
 2: (  )
 3: (  )
 4: (  )
 5: (  )
!
June 14, 2012, 08:51 AM
<JG>
Absolutely correct, Sloppy coding.

It should have been '&ARRAY.EVAL'

The quotes are required if you have the possibility of a space in the string
June 14, 2012, 09:57 AM
mark66
That's great, thank you both.

Mark


WebFocus 765. iSeries v5r4
June 14, 2012, 01:29 PM
Dan Satchell
Another approach:

-SET &ARRAY = 'A,BB,CCC, D,EE,F F F';
-*
FILEDEF TEMPFILE DISK TEMPFILE.TXT
-RUN
-WRITE TEMPFILE &ARRAY
-RUN
-READ TEMPFILE,&P1,&P2,&P3,&P4,&P5,&P6
-TYPE &P1
-TYPE &P2
-TYPE &P3
-TYPE &P4
-TYPE &P5
-TYPE &P6



WebFOCUS 7.7.05