Reputation: 12560
If you have a for loop such as:
For Each s As String In stringList
..do stuff..
Next
is there some sort of (hidden) iterator or do I have to add an external integer to keep count:
Dim i As Integer
For Each s As String In stringList
..do stuff..
i += 1
Next
Upvotes: 2
Views: 1129
Reputation: 29
Django version 1.0.2 final ...
To enumerate:
{% for item in items %}
<li>{{ forloop.counter }}) {{ item }}</li>
{% endfor %}
Try this to apply a 'rule' to the first item in a loop:
{% if forloop.first %}Starting the loop{% endif %}
Try this to exclude the last item:
{% if not forloop.last %}, {% endif %}
... the last example is great for comma separated lists.
Upvotes: -1
Reputation: 700
From an elegant code view it would be nice if the enumerator was available with .First() and .Last() methods.
Upvotes: 0
Reputation: 6760
Part of the magic of "For Each" is that you don't need to track the loop incrementor yourself. It's done for you. You may read documentation refer to the interface "IEnumerable" and say it must implement "GetEnumerator()" which is true. The GetEnumerator() function provides this magic.
I think that is correct. I'm sure someone will correct me if otherwise.
Upvotes: 0
Reputation: 415735
You definitely don't need to provide your own external integer.
Even so, there is no hidden counter. There is a hidden enumerator. The difference is that not everything that can be iterated is accessible by index. Some times you can't get the next item in a sequence until you've calculated the previous item. So an enumerator may use a state machine that tracks your current position and returns the next item for each iteration of the loop, rather than using a counter.
Upvotes: 0
Reputation: 29953
A for-each loop will keep track of the enumerator for you.
ForEach s As String In new string[] {"one","two","three"}
Console.WriteLine(s);
Next
(Sorry about the bastardisation of VB and C#, I'm a curly brackets kind of guy - probably best to think of it as pseudo-code)
The code above would output
one
two
three
if it would compile. :)
Upvotes: 0
Reputation: 67068
foreach loops are implemented using an Enumerator which keeps track of the count. If you want to know how many loops you've gone through you need to use a for loop or keep track of the count yourself.
Upvotes: 0
Reputation: 3645
A foreach does not require another counter. It iterates through each string in your list (assuming stringList in your example is just what it sounds like :) ). Unless there is some reason you want to stop doing something to each string in your list (i.e. only process the first 3 strings). That would be the only reason to have a counter inside your foreach loop. I just reread the OP. The other reason is to know how many strings you have processed.
Hope this helps! Theresa
Upvotes: 2
Reputation: 42227
There is no hidden iterator.
The variable i
in your example is redundant.
PS: If you do need i
to keep track of something, I'd initialise it before I'd rely on it.
Upvotes: 0
Reputation: 32698
There is no hidden iterator, you would have to use the code in your second example or revert back to a regular for
loop.
Upvotes: 9