rnso
rnso

Reputation: 24565

Parallel list assignment in Red language

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.

Upvotes: 3

Views: 111

Answers (1)

sqlab
sqlab

Reputation: 6436

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

Upvotes: 4

Related Questions