0

What I'm trying to do is create array variable names dynamically, and then with a loop, add the object to its relevant array based on the hash table value being equal to the counter variable.

$hshSite = @{}  # Values like this  CO,1  NE,2  IA,3

$counter = $hshSite.count

For($i = $counter; $i -gt 0; $i--) {
New-Variable -Name "arr$i" -Value @()
}

If $counter = 3, I would create arrays $arr1, $arr2, $arr3

$csv = Import-CSV....

ForEach ($x in $csv) {
   #if $hshSite.Name = $x.location (ie CO), look up hash value (1),
   and add the object to $arr1.  If $hshSite.Name = NE, add to $arr2

I tried creating the dynamic arrays with New-Variable, but having issues trying to add to those arrays. Is it possible to concatenate 2 variables names into a single variable name? So taking $arr + $i to form $arr1 and $arr2 and $arr3, and then I can essentially just do $arr0 += $_

The end goal is to group things based on CO, NE, IA for further sorting/grouping/processing. And I'm open to other ideas of getting this accomplished. Thanks for your help!

2 Answers 2

3

Just make your hash table values the arrays, and accumulate the values to them directly:

$Sites = 'CO','NE','IA'
$hshSite = @{}
Foreach ($Site in $Sites){$hshSite[$Site] = @()}

ForEach ($x in $csv)
 {
   $hshSite[$x.location] += <whatever it is your adding>
 } 

If there's a lot of entries in the csv, you might consider creating those values as arraylists instead of arrays.

$Sites = 'CO','NE','IA'
$hshSite = @{}
Foreach ($Site in $Sites){ $hshSite[$Site] = New-Object Collections.Arraylist }

ForEach ($x in $csv)
 {
   $hshSite[$x.location].add('<whatever it is your adding>') > $nul
 } 
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! Yes, the CSV does have a lot of records. Can you briefly explain the benefits of using an array list rather than an array? Thanks again for your help!!!
It's a performance issue related to how arrays are handled internally. You can't really add to an array. What the += operation does is create a brand new array consisting of the elements of the original array, plus the new element. This creates a lot of process overhead. Array lists don't do that, so it's a few ms faster per operation. As long as it's only a few entries the performance difference is trivial, and not worth giving up the simpler an more intiutive coding the array method provides. When you start doing a lot of it the difference gets to be substantial.
2

You could quite easily do add items to a dynamically named array variable using the Get-Variable cmdlet. Similar to the following:

$MyArrayVariable123 = @()
$VariableNamePrefix = "MyArrayVariable"
$VariableNameNumber = "123"

$DynamicallyRetrievedVariable = Get-Variable -Name ($VariableNamePrefix + $VariableNameNumber)
$DynamicallyRetrievedVariable.Value += "added item"

After running the above code the $MyArrayVariable123 variable would be an array holding the single string added item.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.