Solved

search an array

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
A procedure for exporting installed hotfix details of remote computers using powershell
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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