Andrey Marchuk
Andrey Marchuk

Reputation: 13483

Remove items from array dynamically

I have a huge array of XML nodes, sometimes up to 400k items. I use where-object to to select items based on Type attribute. My problem is that it takes quite some time to walk over these items and it would be easier if I could remove items I have just selected from array, so that array would be smaller for next object. Here's what I'm doing:

$subset = $items | Where-Object {$_.Type -eq "2"}
'prosess ItemType 2'

$subset = $items | Where-Object {$_.Type -eq "4"}
'prosess ItemType 4'

$subset = $items | Where-Object {$_.Type -eq "8"}
'prosess ItemType 8'

Problem is that every time I have to iterate over $items again, instead of iterating over what is remaining from previous where-object

It also doesn't help that where-object is a single thread operation and is executed only on one core

What I was thinking about is something like this, but I'm not sure if it will perform better:

$subset = $items | Where-Object {$_.Type -eq "2"}
$items = $items | Where-Object {$_.Type -ne "2"}
'prosess ItemType 2'

$subset = $items | Where-Object {$_.Type -eq "4"}
$items = $items | Where-Object {$_.Type -ne "4"}
'prosess ItemType 4'

$subset = $items | Where-Object {$_.Type -eq "8"}
'prosess ItemType 8'

Upvotes: 0

Views: 405

Answers (1)

Shay Levy
Shay Levy

Reputation: 126732

Have you tried the Select-Xml cmdlet or the XmlDocument.SelectNodes method? Querying XML via its native methods(using XPath), should perform much faster than Where-Object.

Upvotes: 2

Related Questions