0

Here my example code:

$old = @("a_1","b_2","c_3","d_4","e_5")
foreach ($f in $old)
{
$spl = "$f".Split("_")
$spl[0]
}

Output is as expected array of 'a', 'b','c','d','e'. But I want to declare it to a variable so I can use it as new list for another foreach loop. If I try:

foreach ($f in $old)
{
$spl = "$f".Split("_")
$ls = $spl[0]
}
$ls

the output is just 'e'. Any ideas ?

1 Answer 1

2

You can assign the entire output stream from the foreach loop to $ls like this:

$old = @("a_1","b_2","c_3","d_4","e_5")

$ls = foreach ($f in $old)
{
  $spl = "$f".Split("_")
  $spl[0]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.