This question already has answers here:
Creating a Jagged\Multidimensional array with a single array inside of it
(1 answer)
Return Multidimensional Array From Function
(3 answers)
Append an Array to an Array of Arrays in PowerShell
(2 answers)
Closed 4 years ago.
Issue
I am trying to reconstruct an array from the parts that constitute it but I am failing to obtain the same result. I instead end up with a single-dimension array with all the values from the respective arrays.
Question
How can I make sure that $brray is populated with the individual arrays and not only the values that they contain?
MWE
$array = #(("a1","a2"),("b1","b2"))
$a = #("a1","a2")
$b = #("b1","b2")
$brray = #()
$brray += $a
$brray += $b
function test(){
Param(
[string[]]
$array
)
return $array
}
test($array)
test($brray)
Output
$array
a1 a2
b1 b2
$brray
a1
a2
b1
b2
Related
This question already has answers here:
PowerShell outputting array items when interpolating within double quotes
(3 answers)
Closed 8 months ago.
$nameonly = #("") * 20
$i=1
do
{
$nameonly[$i] = "A$i"
$i++
}
until ($i -eq 10)
write-host "$nameonly[4]"
When i run the code i expect the output to be: A4 but instead i get:
A1 A2 A3 A4 A5 A6 A7 A8 A9 [4]
`
Try removing the quotation marks in your final command.
So it should be write-host $nameonly[4]
This will get you the indexed value in position 4 of the array.
You can also skip the write-host command.
Invoking the variable $nameonly[4] will output to the console.
If you use quotation marks then it will convert the whole array into a string and output the entire array content as a string object.
This question already has answers here:
How to concatenate array of integers into comma separated string
(4 answers)
Closed 5 years ago.
I found this snippet for doing a string to array:
$c = "2,3,4,5,6,7,10..12".split(',') | % {iex $_}
How would I do the reverse to convert $c back to a string like "2,3,4,5,6,7,10,11,12"? I of course don't require to abbreviate it back to "2..7,10..12".
You are looking for -join:
$c -join ','
Assume there is an array in variable $a that is created like this,
$a = ,#(1,2,3)
$a += ,#(4,5,6)
$a += ,#(7,8,9)
$a += ,#(10,11,12)
I want to extract part of the array, say $a[1] and $a[2], into another variable, say, $b such that,
$b[0] = #(4,5,6)
$b[1] = #(7,8,9)
I can use a simple for loop to do the task, but I am thinking if there is a more 'elegant' way to do this... may be a one-liner?
Thanks in advance.
You can use the Range operator to slice the array:
$b = $a[1..2]
It is worth noting the Range operator supports dynamic values - very useful when you want to work out your range dynamically, for example:
$a = #(0,1,2,3,7)
$b = #(4,5,6)
$twotoseven = $a[($a.Length-($a.Length-2))..($a.Length-2)] + $b + $a[-1]
Output:
2 3 4 5 6 7
I have an example of a program that creates an array, and then attempts to assign the value of that array multiple times into another array as a multidimensional array.
$a =#(0,0,0)
$b = #($a,$a,$a)
$b[1][2]=2
$b
'And $a is changed too:'
$a
The output is:
PS E:\Workarea> .\what.ps1
0
0
2
0
0
2
0
0
2
And $a is changed too:
0
0
2
So in this instance, the variable is actually pointing to the original variable. This is very unexpected behavior. It's rather neat that one can do this, although I never did use unions that much in my C programming. But I'd like a way to actually just do the assignment of the value, not of the variable.
$b = #($a.clone(),$a.clone(),$a.clone())
I guess would work, but something tells me that there may be something a little more elegant than that.
Thanks for the input.
This is PowerShell 2.0 under Windows 7 64-bit.
To assign the values of $a to $b instead of the reference to $a, you can wrap the variable in $(). Anything in $() gets evaluated before using it in the command, so $($a) is equivalent to 0, 0, 0.
$a =#(0,0,0)
$b = #($($a),$($a),$($a))
$b[1][2]=2
$b
'$a is not changed.'
$a
Be careful in PowerShell ',' is not the enumerator operator, but an array aoperator. The thing you present as multidimensional array is in fact an array of array, you'll find here under the definition of a multidimensional array :
$a= new-object ‘object[,]’ 3,3
$a[0,2]=3
PS > for ($i=0;$i -lt 3;$i++)
>> {
>> for($j=0;$j -lt 3;$j++)
>> {
>> $a[$i,$j]=$i+$j
>> }
>> }
Everything work here as $b is an array of reference.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Comparing two arrays & get the values which are not common
I wanted a logic to get uncommon items from an array, for example:
$a=#(1,2,3,4,5,6)
$b=#(1,2,3,4,5,7,9,10)
I want the output $c to be 6 which is the missing element in $b array, priority should be only given to the array contents of $a.
Can anyone please help me out with this?
Thanks!
Either empo's approach, or
$a1=#(1,2,3,4,5,8)
$b1=#(1,2,3,4,5,6)
Compare-Object $a1 $b1 |
Where-Object { $_.SideIndicator -eq '<=' } |
Foreach-Object { $_.InputObject }
returns 8
$c = $a | ? {!($b -contains $_)}
The priority will be given to the variable you "pipe".