Home Dashboard Directory Help
Search

multi-dimensional array conversion fails silently from 1-dimensional array by James Manning


Status: 

Active


1
0
Sign in
to vote
Type: Bug
ID: 789829
Opened: 6/10/2013 10:59:43 AM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

Hopefully this isn't PEBKAC - it may very well be that I'm just forgetting/missing some part of PowerShell behavior and this is By Design, but it at least surprised me if so. :)

I can create a multi-dimensional array with the array static method fine:

***********
PS C:\> $arr2d = [array]::CreateInstance([int], 2, 3)
PS C:\> $arr2d.rank
2
PS C:\> gm -in $arr2d
TypeName: System.Int32[,]
***********

And trying to implicitly convert a 2d to 3d fails with a dimension count mismatch. I *assume* this is By Design, but I'm not actually sure - either way, it's not the actual 'bug' I'm filing, just showing this part of the behavior:

***********
PS C:\> [int[,,]] $arr3d = $arr2d
The specified arrays must have the same number of dimensions.
At line:1 char:1

PS C:\> $error[0].Exception.ToString()
System.RankException: The specified arrays must have the same number of dimensions.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at System.Array.CopyTo(Array array, Int32 index)
at System.Management.Automation.LanguagePrimitives.ConvertRelatedArrays(Object valueToConvert, Type resultType, Boolean recursion, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConversionData`1.Invoke(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProvider, TypeTable backupTable
at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData)
at System.Management.Automation.PSVariableAttributeCollection.VerifyNewAttribute(Attribute item)
at System.Management.Automation.PSVariableAttributeCollection.InsertItem(Int32 index, Attribute item)
at System.Management.Automation.PSVariable..ctor(String name, Object value, ScopedItemOptions options, Collection`1 attributes)
at System.Management.Automation.VariableOps.SetVariableValue(VariablePath variablePath, Object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts)
at System.Management.Automation.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
***********

And I can create a single-dimensional array either leaving the type implicit or making it explicit:

***********
PS C:\> $arr = 1,2,3
PS C:\> gm -in $arr
TypeName: System.Int32[]
PS C:\> [int[]] $arr = 1,2,3
PS C:\> gm -in $arr
TypeName: System.Int32[]
***********

However, when having PowerShell attempt to create or set a multi-dimensional array (whether 2d, 3d, or higher) from a single-dimensional array, it silently 'fails' (again, assuming this isn't By Design) with the resulting object not matching the actual type declared/intended:

***********
PS C:\> [int[]] $arr1d = 1,2,3
PS C:\> $arr1d.rank
1
PS C:\> gm -in $arr1d
TypeName: System.Int32[]
PS C:\> [int[,]] $arr2d = $arr1d
PS C:\> $arr2d.rank
1
PS C:\> gm -in $arr2d
TypeName: System.Int32[]
PS C:\> [int[,,]] $arr3d = $arr1d
PS C:\> $arr3d.rank
1
PS C:\> gm -in $arr3d
TypeName: System.Int32[]
***********



OS/PS info:
********
Win7 x64

PSVersionTable:

Name                         Value
----                         -----
PSVersion                     3.0
WSManStackVersion             3.0
SerializationVersion         1.1.0.1
CLRVersion                     4.0.30319.18047
BuildVersion                 6.2.9200.16481
PSCompatibleVersions         {1.0, 2.0, 3.0}
PSRemotingProtocolVersion     2.2
********




Details
Sign in to post a comment.
Sign in to post a workaround.