1

As a part of a script for modifying a CSV, I am checking against a column subCatDesc to determine a Team based on predetermined values stored in an array.

$grocerySubTeams = "Grocery","Frozen","Bulk","Dairy"

function identifyTeam() {
foreach ($row in $csv) {
    if ($row.subCatDesc -contains $grocerySubTeams) {
        $row.team = "Grocery"
    }
  }
}

When running the script with this the Team column remains empty. However if I explicitly spell out what I want like below, it works.

function identifyTeam() {
foreach ($row in $csv) {
    if (($row.subCatDesc -like "Grocery") -or ($row.subCatDesc -like "Frozen") -or ($row.subCatDesc -like "Bulk") -or ($row.subCatDesc -like "Dairy")) {
        $row.team = "Grocery"
    }
  }
}

I've tried different comparison operators against the array instead of -contains, but none work.

5
  • Are you trying to compare one item to more than one, or more than one to one? Commented Aug 10, 2017 at 17:45
  • 1
    Try -in, in place of -contains Commented Aug 10, 2017 at 17:48
  • So the column subCatDesc can be either Grocery, Bulk, Frozen, or Dairy. If it matches any of those 4 items then the column Team should be set to Grocery. Does that clarify it for you? Commented Aug 10, 2017 at 17:49
  • 1
    Mark Wragg's suggestion fixed it. Thank you. Commented Aug 10, 2017 at 17:50
  • @BryceJenkins Get-Help about_Comparison_Operators -ShowWindow. -in is for one-to-many, and -contains is its inverse Commented Aug 10, 2017 at 17:58

2 Answers 2

1

It is the other way around:

  $grocerySubTeams -contains $row.subCatDesc
Sign up to request clarification or add additional context in comments.

1 Comment

Actually just switching the comparison operator from -contains to -in like Mark Wragg suggested solved it, as I am comparing the values of $row.subCatDesc to the pre-defined array of $grocerySubTeams.
0

The answer given by Mark Wragg in the initial comments of switching the comparison operator to -in resolved the behavior I was seeing.

if ($row.subCatDesc -in $grocerySubTeams) {

is the proper syntax for comparing the single value of the cell to the many items of the array.

Comments

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.