# Generating a List of Indexed Variables with "=" sign [closed]

I want to generate variables in the form of: r1 = number, r2 = number, r3 = number, etc

The 'numbers' are not necessarily the same and come form a previous calculation.

This is what I have:

z = 5;
v = ToExpression["r" <> ToString[#]] & /@ Range[z]
{r1, r2, r3, r4, r5}


What I want to get is: {r1=, r2=, r3=, r4=, r5=} but the variables are equal to calculated values.

I want flexibility in how many variables each calculation will take. I have been trying ideas I've found here on SE, but can't get further than this.

I can brute-force this and just enter every new variable each time, but if I end up with a large number of variables this won't work.

Anyone have a way to have this work?

• I do not understand what you want. Symbol name can't have equal sign in it. I suggest you include a more complete description of what you are actually trying to achieve (eg. how do you calculate values amd how do you want to use them further). Aug 8 at 21:03
• Say I set some index, say n = 5 and I have a variable "r" that gets calculated prior, so I want to generate each "r" in order with the index and what it's equal to: r1=2, r2=3, r3=4, r5=5 etc
– fiz
Aug 8 at 21:07
• You have z, v, r1, r2 ... in your code. Where did n come from now? Just post the actual code you are using. Aug 8 at 21:09
• Looks like an XY problem to me. Aug 8 at 21:14
• @fiz I am a little confused why would want to make a bunch of variables with different names and set them equal to elements in a list, especially if it was a large list. Why not just index on the list whenever you need a certain value from it?
– ydd
Aug 8 at 21:30

If I understand correctly, you have several options. Let's use some dummy data as your previously calculated data.

previouslyCalculated = Range[5, 10]


Option 1: Assign the list itself to a symbol, and do your indexing with Part:

r = previouslyCalculated;
r[]
(* 6 *)


Option 2: Create down-values for a symbol:

MapIndexed[(s[#2[]] = #1) &, previouslyCalculated]
s
(* 6 *)


Option 3: Generate symbols programmatically and assign them:

Evaluate[Symbol["t" <> ToString[#]] & /@ Range[Length@previouslyCalculated]] = previouslyCalculated
t2
(* 6 *)


The actual mechanism (MapIndexed versus list assignment) for the latter two could be swapped, or some other mechanism could be used, but the point is to either generate symbol names on the fly or use down-values.

You seem to be wanting something along the lines of option 3, but I think that it's usually the worst of the three options. Option 1 is the best option in most circumstances, and there are cases where option 2 is useful.

Update

Based on one of your comments, it appears that what you are asking for is actually just a display, i.e. you don't actually want to set variables to the values. If that's the case, then something like this might work for you:

MapThread[
StringForm["q = ", ##] &,
{Range[Length@previouslyCalculated], previouslyCalculated}]


Update 2

Based on your comment that "the variable names aren't important", maybe all you need is the indices. Maybe something like TableForm would work:

TableForm[
List /@ previouslyCalculated,