Adding a comma force to create an array:
$x = @(
,@(1,2,3)
)
Simple way:
$x = ,(1,2,3)
Answer from CB. on Stack OverflowIssue making array of arrays (of arrays)
Powershell: Correctly exporting an "array of arrays" as a CSV file
PowerShell: How to create a array with mutilple headings
You want some kind of multi-dimensional data structure, not a simple array. In Powershell, the best way to do what you're going for is to create a custom type and then make an array of it.
$a = @() $item = New-Object PSObject $item | Add-Member -type NoteProperty -Name 'Col1' -Value 'data1' $item | Add-Member -type NoteProperty -Name 'Col2' -Value 'data2' $item | Add-Member -type NoteProperty -Name 'Col3' -Value 'data3' $item | Add-Member -type NoteProperty -Name 'Col4' -Value 'data4' $a += $item # $a[0].Col1 == 'data1'
So now, instead of having just an array, which does not have more than one item per index, you have an array of objects, which are somewhat relational. Does this make sense?
More on reddit.comHow can I build an array of arrays from separate files?
Videos
UPDATE: Just wanted to thank everyone for their suggestions. I've decided to go with an array of hashtables at basically everyone's urging. Using u/MrECoyne 's simple conversion script, I created a function to replace import-CSV functionality which also does the conversion and outputs a nice clean hashtable. After making just a few minor tweaks to my other functions (thankfully I'm not that far into the project yet) everything looks like it's working nicely again!
I'm sure I'll have some questions later on when I try to do to hashtables what I can usually do to Arrays, but based on what I've seen I'll have full functionality, just maybe in a different way (like the GetEnumator() function for example). Thanks again everyone!
Hey folks,
In a project I'm working on I'm utilizing nested arrays. I'm having trouble where it seems like it being array is lost somewhere. I can create a diagram of tables to help if needed, but my first question is where I think the failure starts -
I'm importing a CSV that looks something like this:
| Setting1 | Setting2 | Setting3 |
|---|---|---|
| ValueA1 | ValueA2 | ValueA3 |
| ValueB1 | ValueB2 | ValueB3 |
Once imported (using Import-CSV $csvPath), I get what I want to be an array of arrays and what, at least in the console, looks like one. It looks something like this:
| Setting1 | ValueA1 |
| Setting2 | ValueA2 |
| Setting3 | ValueA3 |
| Setting1 | ValueB1 |
| Setting2 | ValueB2 |
| Setting3 | ValueB3 |
Say I save that output to the variable $results. If I call $results.Length I get 2. If I call $results[0] I only get the section that covers ValueA1-A3, and $results[1] is ValueB1-B3.
Calling $results.Setting1 gives me ValueA1 and ValueB1, and $results[0].Setting1 gives me ValueA1 only. All in all it feels like a proper array of arrays.
However, once I try to run array commands on one element I get nothing. $results[0].Length is blank. Running a foreach loop over $results[0] doesn't work. And, in another function, when I try to take it and add it as a member of a custom object to another array, it's added like this:
ArrayName
@{Setting1=ValueA1; Setting2=ValueA2...}
@{Setting1=ValueB1; Setting2=ValueB2...}
And I can no longer dig into them for the individual values (for example, calling $ArrayName[0].Setting1 is empty, as is calling it over the whole thing.
So I'm sure this is going to lead to a few questions from me but the first is - where is it that I'm going wrong at the start? What's being imported when I run import-CSV on a CSV with more than one row of data, and is there a way to properly import it as an array of arrays (since with only one row of data it would import as an array just fine)?
You do not have to use a loop to perfom this, if you do not want to perform any operations on each IP.
Simply use
Write-Host ($whitelist -join "`n")
This will inject a new line between each element in the array. I have compared the differences below:
$array = @('foo','bar')
write-host ($array -join "`n")
Output:
foo
bar
versus
$array = @('foo','bar')
write-host $array
Output:
foo bar
Hope it helps.
Write-Host $elements -Separator "`n"
https://docs.microsoft.com/previous-versions/powershell/module/microsoft.powershell.utility/write-host?view=powershell-3.0
I'm looking to create a variable that is an array which will have multiple columns. I'll generate each of the columns with a seperate task however i'm unsure how to do this.
example
I want $a to have the following data
Col1 Col2 Col3 Col4
data1 data2 data3 data4
Any help would be appreciated
You want some kind of multi-dimensional data structure, not a simple array. In Powershell, the best way to do what you're going for is to create a custom type and then make an array of it.
$a = @()
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'Col1' -Value 'data1'
$item | Add-Member -type NoteProperty -Name 'Col2' -Value 'data2'
$item | Add-Member -type NoteProperty -Name 'Col3' -Value 'data3'
$item | Add-Member -type NoteProperty -Name 'Col4' -Value 'data4'
$a += $item
# $a[0].Col1 == 'data1'
So now, instead of having just an array, which does not have more than one item per index, you have an array of objects, which are somewhat relational. Does this make sense?
Are you doing just one row of data? You should be able to use a Hash/Dictionary.
$EmpNumbers = @{“John Doe” = 112233; “Dave Davis” = 223344; “Justine Smith” = 334455}
Otherwise, get friendly with all the CSV operations.