3

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.

rnso
  • 23,686
  • 25
  • 112
  • 234

1 Answers1

4

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
sqlab
  • 6,412
  • 1
  • 14
  • 29
  • If i=1, how can I get a list/i ? – rnso Sep 15 '17 at 09:00
  • Also how can I get a list with series of numbers, like 1:10 maybe step 2? – rnso Sep 15 '17 at 09:02
  • Is there a map function also in red to apply a function to all elements of list? – rnso Sep 15 '17 at 09:03
  • have a look at >>help map-each and >>help for – sqlab Sep 15 '17 at 09:06
  • @mso you do know about https://chat.stackoverflow.com/rooms/291/rebol ? – Graham Chiu Sep 15 '17 at 09:21
  • Red does not have an official map-each, but sooner than later it will come. Until then there are some implementations done by users you can search on gitter.im/red/red. Same situation with for. Ask for help on gitter.im/red/help – sqlab Sep 15 '17 at 09:47
  • Is `pick alist i` same as `alist/:i`? Similarly, are `poke alist i 5` and `alist/:i: 5` also same? – rnso Sep 15 '17 at 17:41
  • Is pick alist i same as alist/:i? Yes and no. If i is integer yes, otherwise alist/:i like select alist i. a.s.o. No to the second question – sqlab Sep 17 '17 at 12:16