Jamie_lee
Jamie_lee

Reputation: 331

How can you add values into an arraylist

I have 2 sets of variables

$numbers = "1","2","3"
$records = "A","B","C"

that I would like to join togther so the final output is

 Name                           Value                                  
 ----                           -----                                                                                                                                      
 A                              {1}                                     
 B                              {2}                                     
 C                              {3}

so far I have managed to build the Arraylist

$numbers = "1","2","3"
$records = "A","B","C"

$total = [ordered] @{}
Foreach ($entry in $records){ $total[$entry] = New-Object Collections.Arraylist }

Which gives me

 Name                           Value                                  
 ----                           -----                                                                                                                                      
 A                              {}                                     
 B                              {}                                     
 C                              {}

However when I try add the numbers in I get this result and I can't figure out what I am doing wrong. Please can someone nudge me in the right direction. Thank you.

ForEach ($row in $numbers)

    { $total[$records] += $row}

  Name                           Value                                  
 ----                           -----                                                                                                                                      
 A                              {}                                     
 B                              {}                                     
 C                              {}
 {A, B, C}                      {, , , 3} 

Upvotes: 2

Views: 3666

Answers (2)

RoadRunner
RoadRunner

Reputation: 26315

Nice start. Your current approach just needs to account for when you encounter a new key or an existing key.

The main approach you could take is iterate the records, and first check if the record exists as a key in the hash table. If it does, create a new System.Collections.ArrayList, otherwise add the number to the existing key's ArrayList. This assumes that both arrays are equal length.

Here is one way of doing that:

$numbers = "1","2","3"
$records = "A","B","C"

$total = [ordered] @{}
for ($i = 0; $i -lt $records.Length; $i++) {
    $record = $records[$i]

    # Check if record key exists here
    if ($total.Keys -notcontains $record) {

        # Create new array list for the new record key found
        $total[$record] = New-Object -TypeName System.Collections.Arraylist
    }

    # Add item to array list in hashtable
    [void]$total[$record].Add($numbers[$i])
}

$total

For checking if the keys exists, I used -notcontains to check if the key exists in the hashtable Keys. You can have a look at about_comparison_operators for more information on the available operators available in PowerShell.

Additionally, to add an object to an ArrayList, you can use System.Collections.ArrayList.Add(Object). This method returns the type int, which is the index of the value added. This means that you will get the indices outputted to the screen every time an object is added. To suppress this, I casted void, which ignores the return value. If you want to know other ways of doing this, have a look at this Prevent ArrayList.Add() from returning the index question.

Output:

Name                           Value
----                           -----
A                              {1}
B                              {2}
C                              {3}

Upvotes: 2

Vish
Vish

Reputation: 466

If the braces are un-important, since the value you are displaying is just a hash:

$numbers = "1","2","3"
$records = "A","B","C"
$hash    = [ordered]@{}

for( $i = 0; $i -lt $numbers.Length; $i++ ) {
    for( $j = 0; $j -lt $records.Length; $j++ ) {
        if( $i -eq $j ) {
            $hash[ "$( $records[ $j ])" ] = "$( $numbers[ $i ] )"
            continue
        }
    }
}

$hash

Output:

Name                           Value
----                           -----
A                              1
B                              2
C                              3

Upvotes: 1

Related Questions