sort/group foreach loop results

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 months, 2 weeks ago.

  • Author
    Posts
  • #54555
    Profile photo of Grant Harrington
    Grant Harrington
    Participant

    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...)

  • #54558
    Profile photo of Rob Simmers
    Rob Simmers
    Participant
    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
    
    • #54589
      Profile photo of Grant Harrington
      Grant Harrington
      Participant

      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

  • #54570
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

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

  • #54593
    Profile photo of Anthony
    Anthony
    Participant

    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
  • #54609
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    @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.

You must be logged in to reply to this topic.