2

I'm trying to get data out of an array by using following command:

$newarray = $current_ds2_data -match $codenumber

In this case the $current_ds2_data is an array as a result of the "Import-Csv" command and the $codenumber contains the value I want to search for in the array. This work OK.

Following is an example of the value of $newarray:

P_SYS_InternalName  : #D_OCEV_ABC-
P_OCEV_Price        : 0.15
P_NDS_ValidPN       : 12345678
P_OCEV_PriceUnit    : 
P_NDS_VersionNumber : 1

Now I want to modify the value of the P_OCEV_Price field by doing

$newarray.P_OCEV_Price = 0.2

however this doesn't seem to work. It appears that $newarray.P_OCEV_Price contains no value. Somehow PS doesn't recognize P_OCEV_Price to be a cell of the array.

I also tried using

$newarray.["P_OCEV_Price"] = 0.2

to comply with hash-table formating

Next to this I tried defining the $newarray explicitly as an array or hash-table by using

$newarray = @()

or

$newarray = @{}

So far nothing seems to work. What am I doing wrong??

1 Answer 1

1

Since your $newarray variable is an array, you won't be able to use the simple $newarray.P_OCEV_Price syntax to change that value. Here are two alternate options that may help you, depending on your source data:

# Change the price of the first matching item
$newarray[0].P_OCEV_Price = 0.2

# Change the price for all matching items
$newarray | Foreach-Object { $_.P_OCEV_Price = 0.2 }

In cases like this, I usually like to point out that arrays of size 1 are easy to confuse with single objects in Powershell. If you try looking at $newarray and $newarray[0] with simple output statements the results will probably look identical. It's a good idea to keep GetType() handy in these cases.

# This will show a type of Object[]
$newarray.GetType()

# The type here will show PSCustomObject instead
($newarray[0]).GetType()
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, that does the trick. Thanks so much for your swift response!!

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.