Add `AsCollection` option to the `-Split` Operator
See original GitHub issueSummary of the new feature / enhancement
Currently the -Split
operator returns a string array (String[]
) which immediately unrolls which is not always useful in case you want change the returned strings in a larger stream:
Consider the following list:
$List = 'a,b', 'c,d', 'e,f'
I would like split each item on the comma and add the numbers 1
and 2
in the front of the resulted characters:
1a
2b
1c
2d
1e
2f
Probably the easiest way to do this is reassigning the split item:
- create a loop
- split each item and reassign the results
- make changes to each subitem in the assignment
$List | Foreach-Object {
$Split = $_ -Split ','
'1' + $Split[0]
'2' + $Split[1]
}
…or do things along with the format string operator:
$List | Foreach-Object {
$Split = $_ -Split ','
'1{0} 2{1}' -f $Split
}
Proposed technical implementation details (optional)
If -Split
would optionally return a [Collections.ObjectModel.Collection[String]]
, I could have the following syntactic sugar (wishful thinking):
$List -Split ',',0,'AsCollection' | Foreach-Object {
'1' + $_[0]
'2' + $_[1]
}
In other words:
$SplitAsCollection = # $List -Split ',',0,'AsCollection'
[Collections.ObjectModel.Collection[String]]('a', 'b'),
[Collections.ObjectModel.Collection[String]]('c', 'd'),
[Collections.ObjectModel.Collection[String]]('e', 'f')
$SplitAsCollection | Foreach-Object { # $List -Split ',',0,'AsCollection' | Foreach-Object {
'1' + $_[0]
'2' + $_[1]
}
…or do things along with the format string operator:
# $List -Split ',',2,'AsCollection' | Foreach-Object { '1{0} 2{1}' -f @($_) }
$SplitAsCollection | Foreach-Object { '1{0} 2{1}' -f @($_) }
(where the -f
operator should actually recognize a [psobject] collection: '1{0} 2{1}' -f $_
, see: #14355)
Issue Analytics
- State:
- Created 8 months ago
- Reactions:1
- Comments:6
This is a completely different matter. I fully support you.
I would be careful with such requests. A million scripts have been written where the
-split
operator returns an array of strings. What could be the consequences can not be foreseen.This is the solution for your specific case.