Easily index a list with `for` command
See original GitHub issueIn 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:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top 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 >
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 Free
Top 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
@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.:
And since #14732 can be combined with #14724, you could easily add an index with
foreach ($en, $nl in $List, $Dutch; $i) ...
@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.