Solved

search an array

Posted on 2013-01-21
4
220 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

896 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

14 Experts available now in Live!

Get 1:1 Help Now