1

I have an array ($arr) that contains:

A STRING -->       $arr[0]
*empty space* -->  $arr[1]
ANOTHER STRING --> $arr[2]
*empty space* -->  $arr[3]
LAST STRING -->    $arr[4]

And i would like to delete $arr[1] and $arr[3]. How can i do it?

2 Answers 2

3

There are several answers to this question, if $arr is a System.Array you would have to reconstruct it in order to remove the empty spaces. This is because this type is of a fixed size.

Some examples of how you could do that would be:

PS /> $arr = @(
    'A STRING'
    ' '
    'ANOTHER STRING'
    ' '
    'LAST STRING'
)

# Some examples:
$arr = $arr | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
# OR
$arr = $arr.Where({ ' ' -ne $_ })
# OR
filter RemoveWhiteSpace {
    if(-not [string]::IsNullOrWhiteSpace($_)){ $_ }
}

$arr = $arr | RemoveWhiteSpace
# And more...

PS /> $arr
A STRING
ANOTHER STRING
LAST STRING

Note, Where-Object is not the most efficient way of reconstructing the array but works fine for this example. If the array was much bigger, the most efficient way of filtering the collection would be:

PS /> $arr = @(
    'A STRING'
    ' '
    'ANOTHER STRING'
    ' '
    'LAST STRING'
)

$newArr = foreach($i in $arr)
{
    if(-not [string]::IsNullOrWhiteSpace($i))
    {
        $i
    }
}

PS /> $newArr
A STRING
ANOTHER STRING
LAST STRING

Supposing, $arr is of a dynamic type that allows addition and removal of elements, you could do it like this:

PS /> $arr = [System.Collections.ArrayList]@(
    'A STRING'
    ' '
    'ANOTHER STRING'
    ' '
    'LAST STRING'
)

$arr.RemoveAt('1') # Removes position 1, in turn 
$arr.RemoveAt('2') # 'ANOTHER STRING' is the new position 1

PS /> $arr
A STRING
ANOTHER STRING
LAST STRING

Or like this:

PS /> $arr = [System.Collections.ArrayList]@(
    'A STRING'
    ' '
    'ANOTHER STRING'
    ' '
    'LAST STRING'
)

$arr.Remove(' ')
$arr.Remove(' ')

PS /> $arr
A STRING
ANOTHER STRING
LAST STRING

You can find a lot more information on MS Docs.

Sign up to request clarification or add additional context in comments.

1 Comment

@AbrahamZinala sure, I see the edit now :)
2

Santiago's excellent answer already solves the real problem her (removing the empty entries from your array), so I'll attempt to provide a dogmatic answer to the question "How can I remove the array items at index X and Y" instead:

PowerShell's index expression syntax allows you to slice arrays/list - pass an array of indices to the [] operator and it returns a new array consisting of the items at those indices:

PS ~> $arr = 'a,,b,,c' -split ','
PS ~> $arr[0..4]
a

b

c

That means we can exclude $arr[1] and $arr[3] by passing all possible index values except for 1 and 3:

PS ~> $arr[@(0..($arr.Length - 1)).Where({$_ -notin 1,3})]
a
b
c

So the solution is to assign to $arr a slice of itself that excludes the desired indices:

$arr = $arr[@(0..($arr.Length - 1)).Where({$_ -notin 1,3})]

1 Comment

I've been wondering, is [@(0..($arr.Length - 1)] our go to for what in python would be [:-1]? Why don't we have [0..-1]? :(

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.