Parallel list assignment in Red language

大城市里の小女人 提交于 2019-12-14 03:51:00

问题


I have 2 lists:

alist: [a b c d]
blist: [1 2 3 4]

(In reality they are long lists). How can I assign variables in alist to corresponding values in blist in one go? Hence a becomes 1, b becomes 2 and so on.

I tried:

foreach i alist j blist [i: j]

But it give following error:

*** Script Error: j has no value
*** Where: foreach
*** Stack: 

I also tried:

i: 1
while [true] [ 
    if i > (length? alist) [break]
    alist/i: blist/i  
    i: i + 1
]

But it also does not work:

*** Script Error: cannot set none in path alist/i:
*** Where: set-path
*** Stack: 

alist/i and blist/i return none (on checking with print command).

Similar question are there for other languages also, e.g.: Parallel array assignment in PHP and Parallel assignment in Java? . Thanks for your help.


回答1:


easy way, set one list to the other

>> set alist blist
== [1 2 3 4]
>> a
== 1
>> b
== 2
>> c
== 3
>> d
== 4
>> alist
== [a b c d]
>> reduce alist
== [1 2 3 4]
>> get alist/1    
== 1

and the cumbersome way

>> forall alist [alist/1: blist/(index? alist) ]
>> i: 2
== 2
>> get alist/:i
== 2


来源:https://stackoverflow.com/questions/46234891/parallel-list-assignment-in-red-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!