Solved

search an array

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Utilizing an array to gracefully append to a list of EmailAddresses
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

13 Experts available now in Live!

Get 1:1 Help Now