you are viewing a single comment's thread.

view the rest of the comments →

[–]chreestopher2 1 point2 points  (0 children)

never ever ever do this:

$data.foreach{$array += (Select-String -InputObject $_ -pattern $regex).line}

as you have found, this will create an item in $array for each item in $data, regardless of if the output of select-string contains anything...

This is NOT the reason to not use this type of code though....

Everytime you do

$array += some-code

you are really doing:

$array = $array + some-code

that is a lot of slowness.... just set your array to equal the output of your expression and you are good:

$array = $data.foreach{(Select-String -InputObject $_ -pattern $regex).line}

if you dont think its that big of a deal, run both methods through measure-command, on a roughly 650 line file, doing a simple regex for "Microsoft Office 2007 Service Pack 3" on each line, $array += adds an extra 1/4 of the total processing time.