Setting values of a filtered array

  • Follow


Hi All

I want to read in a csv file, filter it based on the values of two of
the fields and set the value of another field. Here's a simple example
of what I'm trying to achieve:

c:\somefile.csv contents:
firstField,secondField,thirdField
1,2,"somevalue"
2,2,"avalue"
3,1,"somevalue"

# Import file into array
$csv = Import-Csv c:\somefile.csv

# Where secondField = 2 and thirdField = "someValue" set thirdField =
"anotherValue"
$csv | where {$_secondField -eq 2 -and $_.thirdField = "somevalue"} |
<set value of thirdField = "anotherValue">

How can I do this. As you can see, from the example about, I can read
in and filter the array. But I don't know how to then set the value of
the thirdField. I tried set-itemproperty but got the error: "The
WriteObject and WriteError methods cannot be called after the pipeline
has been closed".

Any advice on how to achive this would be appreciated.

Alan T
0
Reply mrboring 6/17/2010 3:40:06 PM

Solution found.

Use a foreach at the end.

Alan T

0
Reply mrboring 6/17/2010 5:50:41 PM


1 Replies
458 Views

(page loaded in 0.049 seconds)


Reply: