0

Let's assume we got the following variables:

var1 = 2
var2 = 8
var3 = 24

I want to do the following, just as a loop:

array A[3]
A[1] = var1
A[2] = var2
A[3] = var3

what i tried but did not work:

array A[3]
do for [i=1:3] {
A[i] = var[i]
}

I wanted to add the index from i to the variable so that it'll be A[1] = var1 etc. It says the variable var would be undefined. Round brackets like var(i) don't work either.

Is there even a possibility to do so? Any hints are appreciated.

1 Answer 1

1

Try this. Check help evaluate. Maybe there are better ways.

Code:

### assign variables to array
reset session

var1 = 2
var2 = 8
var3 = 24

array A[3]

do for [i=1:3] {
    eval(sprintf("A[%d] = var%d",i,i))
}

print A
### end of code

Result:

[2,8,24]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.