Numeric Array/Vector?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Numeric Array/Vector?

Craig Johnson
Is it possible to create an array or vector of values (not variables) to be used in a calculation? Below is a conceptual example....

EXAMPLE: 
ARRAY=[.1,.2,.03]. 
LOOP #X=1 TO 3. 
+    DO IF (GENDER='F') AND STR_VAR="'"||BISSTD(#X)||"'". 
+      COMPUTE TIEBREAKER=DLIST(#X).
+   END IF.
END LOOP. 
EXECUTE.

Results: 
BISSTD1=.1.
BISSTD2=.2. 
BISSTD3=.03.

Notes: Array index increments by 1 just like the variable index.
Reply | Threaded
Open this post in threaded view
|

Re: Numeric Array/Vector?

Mark Miller
Craig,

Create a series of scratch variables

Compute #work1 = 98765.
Compute #work2 = 87654.
Compute #work3 = 76543.
...etcetera
Compute #work10 = 65432.

Create a Vector to refer to those scratch variables
VECTOR #WORK(10).
The program creates the vector #WORK, which refers to 10 scratch 
variables: #WORK1, #WORK2, and so on, through #WORK10. 
Thus, the element #WORK(5) of the vector is the variable #WORK5. 

Using your code example, in your loop
refer to Vector elements as #Work(#x)  

... Mark Miller

On Fri, Aug 31, 2012 at 4:30 PM, Craig J <[hidden email]> wrote:
Is it possible to create an array or vector of values (not variables) to be used in a calculation? Below is a conceptual example....

EXAMPLE: 
ARRAY=[.1,.2,.03]. 
LOOP #X=1 TO 3. 
+    DO IF (GENDER='F') AND STR_VAR="'"||BISSTD(#X)||"'". 
+      COMPUTE TIEBREAKER=DLIST(#X).
+   END IF.
END LOOP. 
EXECUTE.

Results: 
BISSTD1=.1.
BISSTD2=.2. 
BISSTD3=.03.

Notes: Array index increments by 1 just like the variable index.

Reply | Threaded
Open this post in threaded view
|

Re: Numeric Array/Vector?

David Marso
Administrator
That's the general idea however syntactically rather inelegant  
If you define a macro to accept a simple name and enclosed value list you can use pretty friendly syntax.
After you have run the DEFINE .... !ENDDEFINE once in any session you can do it as simply as:
TEMPVEC A [ 1 2 3 4 5].

Note the result can be addressed by index #A(1) or by name  #A1 .


**Note you  will have to adapt the macro if you have negative numbers in the list.
I will refrain from providing additional on that however it is trivial once you understand a bit about how the macro parser works (Yeah it is still fucking broken after all these years -I filed a level 5 bug report against it about 20 years ago and they haven't done a damned thing about it- excuse the RANT but I think it is inexcusable -1.00 is *NOT* 2 separate elements in a list ).
--
DEFINE TEMPVEC (!POS !TOKENS(1) /!POS !ENCLOSE("[","]")) .
!LET !L="" !DO !X !IN (!2) !LET !L=!CONCAT(!L,"X") !DOEND !LET !XL=!LENGTH(!L).
VECTOR !CONCAT("#",!1)(!XL).
DO REPEAT V=!CONCAT("#",!1,1) TO !CONCAT("#",!1,!XL) / X=!2 .
+  COMPUTE V=X.
END REPEAT.
!ENDDEFINE.

SET MPRINT ON PRINTBACK ON.
data list free/xx.
begin data
1
end data.

TEMPVEC A [ 1 2 3 4 5].
PRINT /#A1 TO #A5.

TEMPVEC B [2 .4 .7 .8 .2 .4].
PRINT /#B1 TO #B5.
Mark Miller wrote
Craig,

Create a series of scratch variables

Compute #work1 = 98765.
Compute #work2 = 87654.
Compute #work3 = 76543.
...etcetera
Compute #work10 = 65432.

Create a Vector to refer to those scratch variables
VECTOR #WORK(10).
The program creates the vector #WORK, which refers to 10 scratch
variables: #WORK1, #WORK2, and so on, through #WORK10.
Thus, the element #WORK(5) of the vector is the variable #WORK5.

Using your code example, in your loop
refer to Vector elements as #Work(#x)

... Mark Miller

On Fri, Aug 31, 2012 at 4:30 PM, Craig J <[hidden email]> wrote:

> Is it possible to create an array or vector of values (not variables) to
> be used in a calculation? Below is a conceptual example....
>
> EXAMPLE:
> ARRAY=[.1,.2,.03].
> LOOP #X=1 TO 3.
> +    DO IF (GENDER='F') AND STR_VAR="'"||BISSTD(#X)||"'".
> +      COMPUTE TIEBREAKER=DLIST(#X).
> +   END IF.
> END LOOP.
> EXECUTE.
>
> Results:
> BISSTD1=.1.
> BISSTD2=.2.
> BISSTD3=.03.
>
> Notes: Array index increments by 1 just like the variable index.
>
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"