-3

I want to append every value of list1 in every value of list2, so that this:

let list1 = ['one', 'two', 'three']
let list2 = [' wan', ' tu', ' tri']

will become like this:

:echo result
['one wan', 'two tu', 'three tri']

How could this be done?

2
  • 2
    Er, loop and concat?
    – D. Ben Knoble
    Commented Nov 15, 2018 at 16:58
  • 4
    I downvoted because no attempt was made to solve this on your own and because no research effort has been shown. Where did you look for solutions, what have you found, and why didn't it help you? Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. Commented Nov 15, 2018 at 17:01

2 Answers 2

2

I don't like loops -- they are slow and a pain to debug as there is no "continue until the end of the current loop"

:echo map(range(len(list1)), 'list1[v:val].list2[v:val]')

Note: a zip() function is missing, but it can be emulated if we produce a range of indexes into both arrays. We could also simply transform a copy of list1 and use v:key (Vim 7.2.???) for looping over indices, but I prefer range(len(list1))

2

Because you want to ":echo result", my way would be straightforward: :let result = list1 :map(result, 'result[v:key].list2[v:key]') :echo result

(Because map() does in-place replacement, it requires a little trick for one-liner, like @LucHermitte's answer.)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.