I'm working in powershell with an imported CSV. I have a data set like this:
ID First_Name Last_Nam
-- ---------- --------
2314 Kenny Anderson
21588 Mark Anderson
2547 Ben Andrews
5797 Benjamin Armour
Except with 2000 people and many more columns. Currently the data is stored as a series of hashes in a @{ID = "..",First_Name:"..",Last_Name:".."} and these are stored in a System Object array. I want to store each hash as an index in an array. I want to get the hashtable at that index, but I don't know how to into the System.Object Array. Here's my code:
$csv = import-csv $csv_name
$row = @(0)*csv.length
$hash = @{}
for($i =0; $i -lt $csv.length; $i++){
$row[$i] += $csv[$i]
}
#error: cannot convert "@{ID : "..", First_Name: "..", Last_Name:".." to Systems.Int32
for($i =0; $i -lt $csv.length; $i++){
$csv[$i].psobject.properties | Foreach { $hash[$_.Name] = $_.Value }
$row[$i]+=$hash
}
#error: Cannot convert systems.collections.hashtable into Systems.Int32
I'm looking for a way to index the array so I can get the hashtable at that index. the The first one, with pointers to the hashtables accessible through the array, is what I think would be the easiest to manipulate. If there's an easier way to get a specific hashtable just from the System.Object[] itself, please tell me.
I should add I don't know the names or amount of the columns in advance.
+=is a terrible method to add to an array as it recreates and copies the entire array each time. The proper methods are 1) collecting the loop output$arr = foreach (...) { .....; $element }or 2) ArrayList. You can find more examples yourself.+=is far more readable and clear, which is usually the more desirable trait. It's good to know the performance aspects of different ways of doing something, but to say+=is terrible and improper is a stretch, in my opinion.+=with a loop seems excessive to me. I respect your having an opinion on it I just feel the way you present it (+=is wrong, loop collection is right) could use some nuance. Would you say the same about string concatenation? That it should always be replaced with StringBuilder because+and+=recreates a string every time? (serious question, not being rhetorical)+=with string is different: 1) it's simpler than StringBuilder while the opposite is true for the array case (with loop collection there's no need to initialize an empty array, no need to explicitly add the elements), and 2) compared to arrays strings are typically smaller/simpler, and the contents being a primitive data type is copied much faster.