?
Solved

search an array

Posted on 2013-01-21
4
Medium Priority
?
226 Views
Last Modified: 2013-04-11
Hi there, can't seem to find exactly what I'm looking for to search a array and produce the results I need. Let's say I have an array that that has the following.

User            Number            Color
Bill            1            Blue
Scott            2            Yellow
Jill            3            Green
Bob            1            Purple


I know if a person has Number '1' color should be 'Blue', and '2' should be yellow, and '3' should be green. What I want to find are the elements that aren't matching what I expect to see. So I need to search the array above and I would expect to see Bob, 1, purple.

I know I could create a foreach loop with some else and elseif statements, but is there a simpler way I could accomplish the same by piping the array through some search filter?

Thanks in advance for any suggestions.
0
Comment
Question by:Wylz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:coraxal
ID: 38804268
How are you building your array? (i.e. result of importing TXT/CSV file, etc.)
0
 

Author Comment

by:Wylz
ID: 38804299
Hi coraxal, I am building the array by importing a csv.
0
 
LVL 5

Accepted Solution

by:
coraxal earned 400 total points
ID: 38804348
Ok..a couple of similar options; one using if statements and the other using the where-object cmdlet. I think regardless of the approach you still have to check your rules (1=blue, etc..) so there's no escaping that..

$myarray = Import-Csv C:\scripts\colors.csv

$output = @()

$myarray | % { 

	If (( $_.Number -eq "1" ) -and ( $_.Color -ne "Blue" )){ $output += $_ }
	If (( $_.Number -eq "2" ) -and ( $_.Color -ne "Yellow" )){ $output += $_ }
	If (( $_.Number -eq "3" ) -and ( $_.Color -ne "Green" )){ $output += $_ }
	
}	

if($output -ne $null){

	Write-Output "Non-matching records found"
	$output
	
}else{

	Write-Output "All records match"
	}

Open in new window


$myarray = Import-Csv C:\scripts\colors.csv

$output = @()

$output = $myarray | ? {(( $_.Number -eq "1" ) -and ( $_.Color -ne "Blue" )) -or (( $_.Number -eq "2" ) -and ( $_.Color -ne "Yellow" )) -or (( $_.Number -eq "3" ) -and ( $_.Color -ne "Green" ))}

if($output -ne $null){

	Write-Output "Non-matching records found"
	$output
	
}else{

	Write-Output "All records match"
	}

Open in new window

0
 

Author Closing Comment

by:Wylz
ID: 39071493
ok, great! thanks, Sorry for the late response. Just picking this side project up gain, but thank you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question