question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

implement indexer in loop

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:36 (15 by maintainers)

github_iconTop GitHub Comments

5reactions
mklement0commented, Feb 8, 2021

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 ;

# Parellel array processing
$a = 'foo', 'bar'
$b = 'baz', 'qux'
$c = 'and' 'so on'
foreach ($aElem in $a; $i) { # WISHFUL THINKING
  $bElem, $cElem = $b[$i], $c[$i]
  # ...
}

# Get only every 2nd element
$a = 'foo', 'bar', 'baz', 'quux'
foreach ($aElem in $a; $i) { # WISHFUL THINKING
  if ($i % 2) { continue }
}

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.

3reactions
Jaykulcommented, Apr 7, 2021

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:

foreach($item in @($List); $index) {           
  $item | add-member Index $index -PassThru    
}                                              

Really better than this:

$index=0; foreach($item in @($List)) {         
  $item | add-member Index ($index++) -PassThru
}

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):

foreach($item in @($List)) -counter index {           
  $item | add-member Index $index -PassThru    
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found