I am having an interesting problem in Powershell and I could not find a solution for it. When I google (and find things like this post ), nothing special like what I'm trying to do appears, so I decided to post the question here.
The problem is with multidimensional arrays with the length of the external array. Powershell seems to be very categorical about smoothing arrays such as @( @('A') ) becomes @( 'A' ) . Here is the first snippet (prompt is>, btw):
> $a = @( @( 'Test' ) ) > $a.gettype().isarray True > $a[0].gettype().isarray False
So, I would like to have $a[0].gettype().isarray true so that I can index the value as $a[0][0] (the real world script processes dynamic arrays inside the loop, and I would like to get the values ββas $a[$i][$j] , but if the internal element is not recognized as an array, but as a string (in my case), you start indexing into string characters, as in $a[0][0] -eq 'T' ).
I have some examples with long codes, so I posted them at the end. And, for reference, this is on Windows 7 Ultimate with PSv2 and PSCX installed.
Consider code example 1 . I create a simple array manually using the + = operator. The intermediate array $w smoothed and therefore is not added to the final array correctly. I found solutions online for similar problems, which are mainly related to the comma in front of the internal array, to make the external array not smooth, which works, but again I am looking for a solution that can create arrays inside the loop (array from array of arrays, CSS file processing ), so if I add a leading comma to an array of single elements (implemented as an intermediate array of $y ), I would like to do the same for other arrays (e.g. $z ), but this negatively affects how $z is added to the final array.
Now consider code example 2 . This is closer to the actual problem I am having. When a multidimensional array with one element returns from a function, it is smoothed. This is correct before he leaves the function. And again, these are examples, I'm really trying to process the file without knowing whether the function will return using @( @( 'color', 'black') ) or using @( @( 'color', 'black'), @( 'background-color', 'white') )
Has anyone come across this, and has anyone solved this? I know that I can create infrastructure objects, and I assume that everything will be fine if I create an object [] or a list <> or something else like that, but I dealt with this a bit and something seems to be due be the right way to do this (without having to instantiate true frameworks).
Code Example 1
function Display($x, [int]$indent, [string]$title) { if($title -ne '') { write-host "$title`: " -foregroundcolor cyan -nonewline } if(!$x.GetType().IsArray) { write-host "'$x'" -foregroundcolor cyan } else { write-host '' $s = new-object string(' ', $indent) for($i = 0; $i -lt $x.length; $i++) { write-host "$s[$i]: " -nonewline -foregroundcolor cyan Display $x[$i] $($indent+1) } } if($title -ne '') { write-host '' } }
Code Example 2
function Display($x, [int]$indent, [string]$title) { if($title -ne '') { write-host "$title`: " -foregroundcolor cyan -nonewline } if(!$x.GetType().IsArray) { write-host "'$x'" -foregroundcolor cyan } else { write-host '' $s = new-object string(' ', $indent) for($i = 0; $i -lt $x.length; $i++) { write-host "$s[$i]: " -nonewline -foregroundcolor cyan Display $x[$i] $($indent+1) } } if($title -ne '') { write-host '' } } function funA() { $ret = @() $temp = @(0) $temp[0] = @('p', 'q') $ret += $temp Display $ret 0 'Inside Function A' return $ret
Thanks Matt