2
$\begingroup$

Consider some list of keys

keys={"1","2","3","4"}

and the list of values

vals={1,2,3,4};

I wanted to equate them as

quantity[#]&/@keys=vals

but I get an error

Set::write: Tag Map in (quantity[#1]&)/@{1,2,3,4} is Protected

If setting Evaluate[quantity[#]&/@keys]=vals, it makes quantity[key] unusable after next launches. How to equate the values to keys in the compact way?

.

$\endgroup$
4
  • $\begingroup$ What do you mean by "equate"? You want to test whether they are Equal or you want to set the values of quantity? Also, what do you mean by "it makes quantity[key] unusable after next launches"? $\endgroup$
    – Domen
    May 17, 2023 at 11:51
  • $\begingroup$ @Domen : I want to set the values, and the call them by quantity[key]. If using Evaluate, I do not get the value for quantity in a proper way. $\endgroup$ May 17, 2023 at 11:55
  • $\begingroup$ Clear[quantity]; keys = {"1", "2", "3", "4"}; vals = {1, 2, 3, 4}; Evaluate[quantity[#] & /@ keys] = vals; quantity["1"] returns 1. Why is this not the correct result? $\endgroup$
    – Domen
    May 17, 2023 at 12:08
  • $\begingroup$ @Domen : If you try to evaluate this code the second time (without Clear[quantity]), then it will say something like Set::setraw: Cannot assign to raw object 1.. And then quantity will become unusable. Since I will assign values of quantity to different keys, clearing it would be a risk. $\endgroup$ May 17, 2023 at 12:19

1 Answer 1

3
$\begingroup$

You can use MapThread.

keys = {"1", "2", "3", "4"};
vals = {1, 2, 3, 4};

MapThread[(quantity[#1] = #2) &, {keys, vals}]
$\endgroup$
0

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.