On Fri, 30 Jan 2009, Sven Schreiber wrote:
[moving this over from users to devel]
I guess what I mean is:
in a 'loop for i=1..10' expression, the index i obviously takes
on integer values, and it is clear that you have to do some type
conversion to get strings out of that. Hence in that case $i is
perfectly intuitive.
OK. In relation to what follows, I'm probably far too close to
the design to make any meaningful evaluation. Anyone else want to
come in? Jack? Others? That said, I'll plunge in briefly...
However, in a 'loop foreach i peach pear plum' expression
it...
(a) either looks as if i takes on the names of the series. Then why on
earth do I need to use $i instead of just plain i? Or...
(b) the index i really must be thought as looking up the id numbers of
the involved series, and hence is a number.
I sort of get the point, but notice that the "things" over which
we are looping in "foreach" mode need not be series, with ID
numbers: they can just as well be plain old strings (as in the
"peach pear plum" example in the manual, which BTW alludes to a
popular children's book in the US, "Each Peach Pear Plum").
So the usage of 'i' and '$i' here is designed to be versatile: if
you want to use the iteration number qua number, use i; if you
want the string that i indexes, uses '$i'. If you really want the
string representation of the numerical value of i in such a loop
(which seems to me a distinctly less useful thing in context),
then you'd have to use sprintf.
I can see a possible logical case for using the syntax, e.g.,
each[i]
for the indexed strings. But not only would this be backward
incompatible, it would collide with matrix indexation and
generally create a syntactic mess, given what we've inherited.
The thing is that "$i" is a string-substitution macro. As such,
it can be used in distinctively helpful ways (e.g. stuck in the
middle of the name of a new variable), and as such it shouldn't be
confused with ordinary functions or arrays.
It's true that the _source_ of the string that is substituted for
"$i" differs according to the type of loop, as in
A. loop i=1..10
versus
B. loop foreach i <strings or list>
but to my mind this is just a difference rather than a true
inconsistency: "$i" says, "stick in the string representation of
element i in the set of things we're looping over". In case A
it's a set of integers; in B it's a set of literal strings or
variable names.
Allin.