How to create multiple arrays by a loop

Author Message
adbertram

  • Total Posts : 36
  • Scores: 2
  • Reward points : 19790
  • Joined: 12/30/2009
  • Status: offline
How to create multiple arrays by a loop Monday, November 07, 2011 10:52 AM (permalink)
0
I'm using a SQL module to pull data from a database inside Powershell.  The query returns object instances with two properties; PkgID and Content_ID.   There will be numerous Content_ID which each Content_ID being unique inside numerous PackageIDs.  For example, here's a typical output:

PkgID          Content_ID
UNH001          1234
UNH001          5935
UNH004          9532
UNH005          58390
UNH004          68402


What I'd like to do is get this output into a hash table with an array for each PkgID with output like this:


PkgID          Content_ID
UNH001          @(1234,5935)
UNH004          @(9532,68402)
UNH005          @(58390)


I'm thinking I can loop over the result set and do this dynamically but I can't figure out how.  Can anyone shed some light?
 
#1
    gjones

    • Total Posts : 2291
    • Scores: 136
    • Reward points : 97820
    • Joined: 6/5/2001
    • Location: Ottawa, Ontario, Canada
    • Status: offline
    Re:How to create multiple arrays by a loop Sunday, July 01, 2012 11:00 AM (permalink)
    0
    Yes, I know this is an old post, I’m trying to clean up older posts that are unanswered.
    Did you ever figure this out?
     
    #2
      Pvt_Ryan

      • Total Posts : 310
      • Scores: 3
      • Reward points : 77050
      • Joined: 8/25/2009
      • Location: Belfast, UK
      • Status: offline
      Re:How to create multiple arrays by a loop Monday, July 02, 2012 4:50 AM (permalink)
      0
       #### TESTDATA ####
       $results = @()
      $pkgs = "" | Select PkgID, ContentID
      $pkgs.PkgID = "UNH001"
      $pkgs.ContentID = "1234"
      $results += $pkgs
       $pkgs = "" | Select PkgID, ContentID
      $pkgs.PkgID = "UNH001"
      $pkgs.ContentID = "5935"
      $results += $pkgs
       $pkgs = "" | Select PkgID, ContentID
      $pkgs.PkgID = "UNH004"
      $pkgs.ContentID = "9532"
      $results += $pkgs
       $pkgs = "" | Select PkgID, ContentID
      $pkgs.PkgID = "UNH004"
      $pkgs.ContentID = "58390"
      $results += $pkgs
       $pkgs = "" | Select PkgID, ContentID
      $pkgs.PkgID = "UNH005"
      $pkgs.ContentID = "68402"
      $results += $pkgs
       #### END TESTDATA ####
       ########### You need the following bit #########
       $newresults = @{} # Create the hashtable
       $results | %{
        $arr = @()
        if ($newresults.containsKey($_.PkgID)) {  # If we have already added the pkgid to the table get it
          $arr = $newresults[$_.PkgID]                 # Retrieve the array
          $arr += $_.ContentID
          $newresults.Set_Item($_.PkgID,$arr)    # Overwrite the item with the new data
        } else {                                                     # else create it
          $arr += $_.ContentID
          $newresults.Add($_.PkgID, $arr)
        }
      }
       ######################
       $newresults
        
       

      Citrix Desktop Infrastructure Analyst
      MCTS: SCCM, CCNA

      Blog/Site: http://ninet.org
       
      #3
        w.lewis

        • Total Posts : 1
        • Scores: 0
        • Reward points : 1420
        • Joined: 6/11/2012
        • Status: offline
        Re:How to create multiple arrays by a loop Monday, August 20, 2012 5:41 PM (permalink)
        0
        .
        <message edited by w.lewis on Monday, August 20, 2012 5:43 PM>
         
        #4
          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2013 ASPPlayground.NET Forum Version 3.9