Author Posts

September 23, 2016 at 7:13 pm

I have an array of data and I'm passing it to a foreach loop with a nested if statement.

$fruit = @('apple','orange','banana','grape','grape-orange')
foreach ($f in $fruit) {
	if ($f -like "*apple*") {
		"I have something red, it is a/an $f"
	} #end if
	elseif ($f -like "*orange*") {
		"I have something with orange in it,it is a/an $f"
	} #end elseif
	else {
		"This is neither red or orange, it is a/an $f"
	} #end else
} #end foreach 

Results are
I have something red, it is a/an apple
I have something with orange in it,it is a/an orange
This is neither red or orange, it is a/an grape
I have something with orange in it,it is a/an grape-orange

I'd like the onscreen output to be grouped like this
Each result in "if"
I have something red, it is a/an apple

Each result in "elseif"
I have something with orange in it,it is a/an orange
I have something with orange in it,it is a/an grape-orange

Each result in "else"
This is neither red or orange, it is a/an grape

I understand why the output is ordered this way (it's looking at Apple, evaluating, looking at Orange, evaluating, etc...)

September 23, 2016 at 8:59 pm

foreach ($f in $fruit) {
   switch -wildcard ($f)  {
        "*apple*"{"I have something red, it is a/an $f"}
        "*orange*"{"I have something with orange in it,it is a/an $f"}
        default {"This is neither red or orange, it is a/an $f"}
    }	
} #end foreach 

Output:

I have something red, it is a/an apple
I have something with orange in it,it is a/an orange
This is neither red or orange, it is a/an banana
This is neither red or orange, it is a/an grape
I have something with orange in it,it is a/an grape-orange

September 24, 2016 at 1:47 am

Hey Rob, do you feel like you just did someone's homework? :^)

September 25, 2016 at 6:03 pm

Thanks. That wasn't what I was looking for (switch), rather I was trying to understand if there was a way to "sort" using the "native" scripting syntax when using if/elseif/else (I do use Switch routinely for other situations). My project involves gathering/reporting HKLM Registry values.

I solved it by creating a Custom PSObject inside each of my if/else/else blocks and the was able to sort those results.

$IfObject
I have something red, it is a/an apple
$ElseifObject
I have something with orange in it,it is a/an orange
I have something with orange in it,it is a/an grape-orange
$ElseObject
This is neither red or orange, it is a/an grape

September 25, 2016 at 10:55 pm

foreach just loops through the variable in the order that the variable is in. You won't be able to "sort" the output inside of a foreach loop because your $f only contains one item at a time.

You would need to either sort your variable before it hits the foreach or inside your foreach loop get the results of the if statement and output it to another variable then do the sort on that variable

$fruit = @('apple','orange','banana','grape','grape-orange')
$sortme = @()
foreach ($f in $fruit) {
  if ($fruit -like "*apple*") {
    $sortme += "I have something red, it is a/an $f"
  } #end if
  elseif ($fruit -like "*orange*") {
    $sortme += "I have something with orange in it,it is a/an $f"
  } #end elseif
  else {
    $sortme += "This is neither red or orange, it is a/an $f"
  } #end else
  }
  $sortme | Sort-Object

September 26, 2016 at 2:50 pm

@Curtis, Hard to tell if it was an example or a syllabus, but we all have to learn somewhere. Google search has busted many plagiarized content, especially when posting the question with an actual name.