-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Automatic variable for the pipeline index #13772
Copy link
Copy link
Open
Labels
Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeThe bot will ignore these and not auto-closeWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working GroupNeeds a review by the labeled Working Group
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeThe bot will ignore these and not auto-closeWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working GroupNeeds a review by the labeled Working Group
Summary of the new feature/enhancement
Initiate and increase an automatic variable (e.g.
$PSIndex) each time the current object ($_/$PSItem) is assigned to the next object.The thought originated from request #13769; what if there isn't a parameter available to support my index selection requirement?
E.g.: in case I want to get each odd row to reformat it for a readability (or a combination with #13769 or anything else)
I would need to create a custom index. That index needs to be initiated and increased with each iteration, something like:
This is also required in a other situations, as shown in StackOverflow questions along with:
See also: Get index of current item in a PowerShell loop
Examples
If there would be an automated index variable, these solutions could be simplified to:
#13769 NotIndex
Select odd rows
Combine single arrays into columns
Make array elements as the keys of hashtable
Fundamental test (expectation)
Caveats
-Indexand-Skipin theSelect-Objectcmdlet but I have no clue if that is available for every cmdlet in the pipeline.Where{ $PSIndex ... }?) should all refer to a different index:1..5 | % { $PSIndex } | ... | { $PSIndex }(as also the case with$_/$PSItem)