Returning single-element multi-dimensional arrays
now, know functions in powershell not "return" values in way typical programming languages do, rather pass objects down pipeline, however, doesn't affect outcome, , can treated identically way you'd treat calling/returning in other languages.
with advantage can return multiple values, awesome.
however, when i'm writing powershell script process large amount of data, it's logical lay out in multi-dimensional array -- table. example, have been writing series of scripts generate csv data, , successive scripts read in csv data , process in new way.
when reading csv data in, find logical store multi-dimensional array. each line index of "outer" array, , each value index "inner" array.
however, there times when csv file has 1 line in it.
when try , call function takes filename of csv file, , parses appropriate multi-dimensional array, , returns array, if "outer" array has 1 element (i.e. file had 1 line), powershell "flattens" array, turning one-dimensional array, proceeds totally break remainder of code, , generate sorts of "index null array" errors (for obvious reasons).
trying logicize pipelining paradigm has left me dry. i've tried everything, casting returned value array, piping foreach, et cetera, no matter what, multi-dimensional arrays returned more 1 index fine, if csv file had 2 lines, returned value would work perfectly, everytime multi-dimensional array 1 index returned, "flattened".
this problem occurs single-dimensional arrays 1 value, casting them arrays fixes problem (because not arrays), however, since "flattened" multi-dimensional array single-dimensional array, casting them arrays has no effect (because arrays, not many dimensions logic of code requires).
any solutions?
to me obvious flaw otherwise flawless scripting language.
after last post -- comparing powershell php -- thought myself "i can in c!", @ point started usual rage how pointers handled gracefully in c, , handled clumsily in java.
then realized can accomplish goal in c-esque way:
ps c:\> function multiarray ($inputarray) {
>> $inputarray.add(@(1,2,3)) | out-null
>> }
>>
ps c:\> $a=new-object system.collections.arraylist
ps c:\> multiarray $a
ps c:\> $a
1
2
3
ps c:\> $a[0][0]
1
ps c:\> $a[0][1]
2
ps c:\> $a[0][2]
3
ps c:\>
Windows Server > Windows PowerShell
Comments
Post a Comment