2

How to do something equivalent to this:

In: A = [ 1 2 3 ]
In: B = 2 * A
In: B
Out: [ 2 4 6 ]

This method gets part of the way:

In: do for [i in "1 2 3"] { print 2*i }
Out:
2
4
6

But I want to return another list/array that can be used in further operations.

1 Answer 1

1

As you already found out, using space-delimited words is the only way to simulate arrays. So you must format the output again as a string in which the single entries are separated by spaces:

out = ''
do for [i in "1 2 3"] { 
    out = out . sprintf('%d ', 2*i)
}

print sprintf('%d entries: %s', words(out), out)

This prints

3 entries: 2 4 6

If using floats, you must use e.g. '%f' to format the output:

out = ''
do for [i in "1.1 2.2 3.3"] { 
    out = out . sprintf('%f ', 2*i)
}

print sprintf('%d entries: %s', words(out), out)

words counts the words in a string, and you can use word to extract a certain word from the string (starting from 1):

print word(out, 2)

4

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.