Solved

search an array

Posted on 2013-01-21
4
221 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
  • 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 100 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now