powershell - Add an array to an array list - Stack Overflow
arrays - Add new set of values to ArrayList - Stack Overflow
powershell - How can you add values into an arraylist - Stack Overflow
ArrayList.Add Method
Videos
$var = New-Object System.Collections.ArrayList
$var.Add(@{"ip_prefix" = "0.0.0.0/24"; "region" = "GLOBAL"; string = "Something"})
$var.Add(@{"ip_prefix" = "127.0.0.1/32"; "region" = "GLOBAL"; string = "SOMETHING"})
$var
$var | %{ Write-Output "$($_.ip_prefix), $($_.region), $($_.string)" }
Or:
$var = @()
$var += @{"ip_prefix" = "0.0.0.0/24"; "region" = "GLOBAL"; string = "Something"}
$var += @{"ip_prefix" = "127.0.0.1/32"; "region" = "GLOBAL"; string = "SOMETHING"}
Should do the job
$obj = New-Object PSObject -Property @{
ip_prefix = "0.0.0.0/24"
region = "GLOBAL"
string = "Something"
}
$var+= $obj
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}
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
Hey guys
I am trying to add Table Objects into an ArrayList. This always worked but suddenly I am getting an error.
#create table class
class Table
{
[String] $table_name
[String] $content_type
[String] $finishedOn
}I add the Table-Objects like this:
$tables = New-Object System.Collections.ArrayList
while ($reader.Read())
{
$table = New-Object Table
$table.table_name = $reader["table_name"];
$table.content_type = $reader["content_type"];
$table.finishedOn = $LoadDate;
[void]$tables.Add($table);
}The error I'm getting is:
System.Management.Automation.RuntimeException Method invocation failed because [Table] does not contain a method named 'Add'.
I don't really get why PS gives me this error, since I'm using the Add-Method of the ArrayList, and am adding a Table-Object. As far as I know I don't need an add-Method for Table-Objects. Also it's very strange that I never got this error before while trying out my script, but now it's appearing every time.
Do I need to create an Add-Method for my Table-Class? And if so, how would I go about this?
EDIT: Fuck this. When I use Get-Member -InputObject $tables It sometimes tells me $tables is an array, sometimes it tells me it's an arraylist and sometimes it tells me it's a table. It's infuriating (though interesting). I'll try if I can pinpoint where it changes its object-type.
EDIT2: Thanks everyone! I'm really happy there is such a nice and active PowerShell community here on reddit. I hope I can come back to you if I got any other questions and as I'm learning more, hopefully I'll be able to answer some questions of other users! :)