# how can I generate a sequence of assignments?

I want to make a sequence like follows

r[1]=
r[2]=
r[3]=
r[4]=
....
r[n]=


The above r[i] represents coordinates which I have to type in manually one by one. So I make such a sequence to make the typing easier and more confortable. (even without having to type the =, I am so lazy, I admit). If the problem involve n coordinate, I just generate a sequence of length n. So I naively write as follows

Table[r[i]= ,{i,1,n}]//TableForm


But with = there, I can't make such a list. So how could I generate a sequence of such incomplete expressions (like the above expressions, the = has no right side, this makes Mathematica can't table it)?

-
This is not a good way to write software. –  Nasser Jan 4 '13 at 3:16
@Nasser hey! your comment above should be kept in some distinguished place! –  belisarius Jan 4 '13 at 4:52
@Nasser Hi! Nasser. Thank you for your suggestion. But you leave a sentence without any further explanation. And I don't know why my way of typing input is not proper. The "r[i]" represents coordinate, when doing a specific calculation, I have to input initial coordinate manually one by one according to real situations. I can't find a better way to do it. Could you suggest a better way for me? –  matheorem Jan 4 '13 at 6:57
It sounds like you're building a data entry user interface. There might be a more natural way to do this, but it would help to describe more what you're doing. E.g. is the size of the list, n, known ahead of time or can it vary? –  Joel Klein Jan 4 '13 at 7:12
@JoelKlein n is also dependant on specific question. But it won't vary within the same question. What I want to do is actually a plot on a zigzag line, and r[i] represent the turning point –  matheorem Jan 4 '13 at 7:21

I'm not sure what you're trying to do here and most probably Nasser's admonition rings true, but perhaps you want something like this?:

Do[
CellPrint @
Cell[BoxData[RowBox[{RowBox[{"r", "[", i, "]"}], "="}]], "Input"],
{i, 5}
]


Alternatively you might make use of \[Placeholder] and Defer:

Do[CellPrint @ ExpressionCell[Defer[r[#] = \[Placeholder]], "Input"] & @ i, {i, 5}]


Function (short form &) is needed to get the value inside Defer as the latter has a Hold attribute. See Using pure functions in Table for more. Array as used below is more practical in this specific case, but I wanted to demonstrate using Do or Table as well in case your indexes are not the natural numbers.

Better still with all of these inputs in a single Cell:

Cell[
BoxData[RowBox[
Array[ToBoxes @ Defer[r[#] = \[Placeholder]] &, 5] ~Riffle~ "\n"
]],
"Input"
] // CellPrint


Now the advantage of using \[Placeholder] becomes clear: you can use Tab to move between input fields.

You may not want to use this kind of input method at all. You can make assignments like this:

{r[1], r[2], r[3], r[4], r[5]} = {0.0, 0.2, 0.4, 0.6, 0.8}


You could get the left-hand side list with simply Array[r, 5] (assuming no prior assignments).

Depending on your application you could also store one list (array):

s = {0.0, 0.2, 0.4, 0.6, 0.8};


Then use Part to access elements:

{s[[2]], s[[5]]}

{0.2, 0.8}


Perhaps you want each value on a separate line. You can use Ctrl+Enter to create an ad hoc table:

When evaluated the data looks like this:

{{0.}, {0.2}, {0.4}, {0.6}, {0.8}}


You could use Join or Flatten to get back to the simple list form used with s.

You could also make assignments for a given list of values as m_goldberg shows.

-
Yeah! That's exactly what I want. I have more questions. First, why there is ToString before i ? I found that it also works without ToString. Second, why there is two RowBox? I found that with only one RowBox, that is to write like RowBox[{"r", "[", i, "]", "="}], will give result with a colored left bracket. Though the colored left brackets seems harmless, because I can set values normally. But I still curious about it. And finally both you and Nasser point it out that my way of input is not good. I leave a message for Nasser, would you take a look at it? –  matheorem Jan 4 '13 at 6:57
@user I was in a hurry last night. You're right, ToString is unnecessary here. Also, I got the Box structure quickly by typing r[1]= in a cell and using Cell > Show Expression (Ctrl+Shift+E on Windows), which is typically the simplest way to get the underlying Box form that Mathematica (or more specifically the FrontEnd) uses. See this question for another. I'll add a couple of things to my answer that I hope you'll find useful or informative. –  Mr.Wizard Jan 4 '13 at 15:08
Hi,Wizard. Sorry for this late comment. Thank you very much for your elabrate and perfect answer. From you, I learned the cell structure. –  matheorem Jan 31 '13 at 4:18
@user15964 You're welcome, and thanks for the Accept. –  Mr.Wizard Jan 31 '13 at 7:32

I'm guessing the OP is looking a way to generate a number of indexed assignments from a list of data elements. If so, then maybe something like the following will work for the OP:

Clear[data, r];
data = RandomInteger[{0, 99}, 5];
MapIndexed[(r[#2[[1]]] = #) &, data];

?r


Globalr
r[1]=14
r[2]=95
r[3]=39
r[4]=26
r[5]=60

-

Here is a simplistic approach, I'd be interested in how it does or does not meet your requirements:

r={1,2,3,4,5,6,7};

{r[[1]],r[[5]],r[[2]]}


{1, 5, 2}

If you wanted a more structured input format, try setting the value of r equal to the result of the first part of an input table (via menus, Insert->Table/Matrix->New ) which gives you something like this:

And then parts of r can be accessed as per the above example with r[[n]].

-
damm I was just writing very same answer. Yes, the OP should just use a simple list` –  Ajasja Jan 4 '13 at 10:07
@Ajasja It certainly seems a possible solution. Sorry, I know that can be frustrating :( –  image_doctor Jan 4 '13 at 10:11
@image_doctor Thank you, Image_doctor! I know what you mean, ,I actually want the input process more intuitive. I have edited my question to make myself clear. –  matheorem Jan 4 '13 at 11:03
@image_doctor No, no, I don't mean this as a serious comment (just forgot to end with a smiley:) –  Ajasja Jan 4 '13 at 16:16