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.

Add `AsCollection` option to the `-Split` Operator

See original GitHub issue

Summary 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:open
  • Created 8 months ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
237dmitrycommented, Jan 11, 2023

Add AsCollection option to the -Split Operator.

This is a completely different matter. I fully support you.

1reaction
237dmitrycommented, Jan 11, 2023

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.

Besides, the current syntax (as in the example in the summary) is still supposed to work on iterated scalars

This is the solution for your specific case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

about Split - PowerShell
The Split operator splits one or more strings into substrings. You can change the following elements of the Split operation: Delimiter.
Read more >
Split the values by a comma and pass it to a collection ...
[Path] If all comma occurrences finished. Assignment (Assign last value). Variable: {!recordIdDisplay_collection}; Operator: Add; Value: {! ...
Read more >
A Guide on Split () Function in Python
A string is a collection or array of characters in a sequence that is ... The .split() method is a beneficial tool for...
Read more >
Split string with PowerShell and do something with each ...
Any run of whitespace (e.g., multiple adjacent spaces) is treated as a single separator. In PowerShell v4+ an expression-based - and therefore ...
Read more >
Using PowerShell to split a string into an array
Split () function to split the string in multiple words and used index of the array to print the “Sonali” and “Administrator.” This...
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