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)
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?
|
|
|
|
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)
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?
|
|
|
|
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)
#### 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
|
|
|
|
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)
.
<message edited by w.lewis on Monday, August 20, 2012 5:43 PM>
|
|
|
|