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.

Easily index a list with `for` command

See original GitHub issue

In addition to the other requests to more easily iterate through the index of an object list:

There are currently common ways to iterate through the index of an object list:

$List = @(
    [pscustomobject]@{Name = 'One';   Value = 1}
    [pscustomobject]@{Name = 'Two';   Value = 2}
    [pscustomobject]@{Name = 'Three'; Value = 3}
)
For ($i = 0; $i -lt $List.Count; $i++) {
    Write-Host $List[$i].Name '=' $List[$i].Value
}
0..($List.Count - 1) |ForEach-Object {
    Write-Host $List[$_].Name '=' $List[$_].Value
}

Knowing that the For command generates an error when no parenthesis are used:

PS C:\> For $Test
ParserError:
Line |
   1 |  For $Test
     |     ~
     | Missing opening '(' after keyword 'for'.

It might might just generate a (new) index if a object (list) is supplied.

Wishful thinking:

For $List {
    Write-Host $List[$_].Name '=' $List[$_].Value
}

Or using the pipeline:

$List |For {
    Write-Host $List[$_].Name '=' $List[$_].Value
}

Caveat: As with the ForEach-Object, it might be required to force the input to an array: @($List) |For { ...

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Apr 30, 2021

@iRon7, it seem to me that your expanded example - processing the corresponding elements from two (or more) collections - is more elegantly handled by the proposal in #14732; e.g.:

# Wishful thinking.
foreach ($en, $nl in $List, $Dutch) {
    [PSCustomObject] @{
        Number  = $en.Value
        English = $en.Name
        Dutch   = $nl.Name
    }
}

And since #14732 can be combined with #14724, you could easily add an index with foreach ($en, $nl in $List, $Dutch; $i) ...

0reactions
daxian-dbwcommented, May 6, 2021

@iRon7 when it comes to a new command proposal, the guidance we have is that the proposed command could be shipped as a module by the community member. Once the use of that command is proven to be popular, we can start the conversation about whether it should be included in powershell by default.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python | Accessing all elements at given list of indexes
Another efficient way to access multiple elements in a list at given indices is by using the numpy. take() function. Numpy is a...
Read more >
Python List index() with Example
This is the easiest and straightforward way to get the index. The list index() method returns the index of the given element.
Read more >
A command to return the maximum index of a list or array
Hello :smiley:, short form: Let's have something called like lix() that is defined as len()-1, please! def lix(obj, /): return len(obj) - 1 ......
Read more >
Python Find in List – How to Find the Index of an Item or ...
In this article you will learn how to find the index of an element contained in a list in the Python programming language....
Read more >
Index() in Python: The Ultimate Guide [With Examples]
Index () is an in-built method in python that finds an item's index position in a character's string element list of item. Learn...
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