0

Given an array like this

$array = @('A', 'B', 'C', 'D', 'E')

I can append something to the end of each item in the array like so

$array = $array | Foreach-Object {"$_ *"}

And if I want to append something DIFFERENT to each member I can do so like this

$tempArray = @()
$count = 1
foreach ($item in $array) {
    $tempArray += "$item $count"
    $count ++
}
$array = $tempArray

But that use of a temporary array seems... clumsy. I feel like I am missing something that would allow conditionally changing the values of the original array instead. Not adding new items, just changing the value of existing items.

3
  • You are wanting to change or append? What do you hope to end up with? Commented Aug 12, 2020 at 17:23
  • @doug-maurer, I want to append to the end of the current value. So the final array is A 1, B 2, C 3, D 4, E 5 Commented Aug 12, 2020 at 17:25
  • I added example for both appending the value and appending a space and a value. Commented Aug 12, 2020 at 18:33

3 Answers 3

1

you have two options here

1: set the result of the foreach to the variable you are iterating. this will iterate the array and set the result to the same variable.

$count = 1
$array = foreach ($item in $array) {
    "$item $count"
    $count ++
}

2: set an array element like so (note array elements begin by 0 not 1)

$array[2] = 'foo'

or in a loop like like so. this will set each variable at iteration.

$append = 'a'
for($i=0;$i -lt $array.count;$i++){
    $array[$i] = $array[$i],$append -join ' '
    $append += 'a'
}
Sign up to request clarification or add additional context in comments.

1 Comment

Aha. I think the thing I was missing was your first example, setting the $array = to the result of the loop. I had just been doing the loop and trying to set $item =. For more complex conditions where I am not already creating an index variable, this seems better.
1

You don't need to add @(), it's already an array as

$array = 'A', 'B', 'C', 'D', 'E'

If you simply want to append to the string and store back into the array, this is how I would do it. I'm taking advantage of the -Begin, -Process, and -OutVariable parameters of Foreach-Object.

$array | ForEach{$count = 1}{$_ + $count++} -OutVariable array

Output (also stored back in $array)

A1
B2
C3
D4
E5

Now if you did actually want to append a space before the number, then simply add that to the item like this

$array | ForEach{$count = 1}{"$_ " + $count++} -OutVariable array

Output

A 1
B 2
C 3
D 4
E 5

If you need to control the data flow more explicitly I would still lean towards not using for loops. This is equivalent.

$array = 'A', 'B', 'C', 'D', 'E'
$array = 1..$array.Count | ForEach{$array[$_-1] + " $_"}

or you could also write it as

$array = 'A', 'B', 'C', 'D', 'E'
$array = 1..$array.Count | ForEach{"$($array[$_-1]) $_"}

1 Comment

Neat, I didn't know the -outVariable trick. I can see that coming in handy. For my immediate needs I thinkGuenther's first solution below is going to allow me to handle the complex conditionals my real code needs. But all three will have their place in other situations.
0

Something like this seems to be what you're after:

$array = @('A','B', 'C', 'D', 'E')
for ($c=1; $c -le $array.count; $c++) {
    Write-host "$($array[$c-1]) $c"
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.