implement indexer in loop
See original GitHub issueHi guys !
loops in powershell is various and its awesome but it missing an indexer for example:
foreach($index,$item in $list) {
"$index = $item"
}
is more elegant and short:
$index = 0
foreach($item in $list) {
"$index = $item"
$index++
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:36 (15 by maintainers)
Top Results From Across the Web
How to use an index variable with C#'s foreach loop?
A quick way to use an index variable with foreach is to declare one before the loop starts. And then inside the loop...
Read more >c# - How do you get the index of the current iteration of a ...
Select((v, i) => (v, i))) Allows you to access the item and index (i) directly inside the for-loop with tuple deconstruction. – Haukman....
Read more >Looping with indexes
Python's built-in enumerate function is the preferred way to loop while ... We're using i to index the list manually and add one...
Read more >How to Access Index in Python's for Loop
Access Index in Python's for Loop using enumerate() method. The enumerate() is mostly used for loops where it is used to get the...
Read more >Python - Access Index in For Loop With Examples
Use the python enumerate() function to access the index in for loop. enumerate() method is an in-built method in Python, which is a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I like the idea; it complements @iRon7’s proposal to introduce an automatic
$PSIndex
variable in the pipeline: #13772@doctordns, it’s helpful syntactic sugar; just a couple of use cases off the top of my head; I’m sure there are more.
Note: The original syntax was changed to reflect the later discussion; the index variable now comes after the iteration expression, separate with
;
As a largely moot aside: Quite some time ago, in #3830, I had proposed allowing syntax such as
foreach ($a) { ... }
- not having to specify an iteration variable explicitly and defaulting to$_
/$PSItem
; the proposal was rejected, but it would have allowed us a unified approach with respect to an automatic$PSIndex
as well.I have to say that whenever I’ve needed something like this, I’ve just changed to a
for(){}
loop, or added a counter variable.I don’t mind the idea of adding syntax to
foreach(){}
to define a counter variable to be incremented for me, but I’m not convinced the savings are worth it. I mean, is this:Really better than this:
Remember that it’s not just about saving you a few keystrokes, it’s also about clarity and readability …
To make it clearer, we could add a parameter (there’s precedent in
switch
):