铁血PowerShell的阵列正在失去一个维度只有一个元素存在, [英] Jagged PowerShell array is losing a dimension when only one element exists

查看:225
本文介绍了铁血PowerShell的阵列正在失去一个维度只有一个元素存在,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的PowerShell功能非常适用于任何输入除 1 。如果我将它传递 1 的输入,将返回一个包含有两个元素 1.1 ,而不是一个单一的元素这本身就是两个元素的数组(1,1)

任何想法如何,我可以做的PowerShell返回交错数组有一个元素本身是一个数组?

 功能getFactorPairs {
    参数($ N)
    $ factorPairs = @()
    $ maxDiv = [数学] ::开方($ N)
    写冗长的最大除数:$ maxDiv
    为($ C = 1; $ C $ -le maxDiv $ C ++){
        $ O = $ N / $ C;
        如果($Ø当量[数学] ::地板($ O)){
            写调试因素对:$ C,$ O
            $ factorPairs + =,@($ C,$ O)#逗号告诉PowerShell来添加定义数组元素在现有阵列,而不是增加数组元素现有阵列
        }
    }
    返回$ factorPairs
}
 

下面是我的测试,它的输出,显示问题。你可以看到,第一个例子(1为输入)返回2的长度,即使只有一个发现的因素对。第二个例子(6作为输入)工作正常,并返回一个长度为2发现了两个因素对。

 〜»(getFactorPairs 1).length
   DEBUG:因子对:1,1
   2

〜»(getFactorPairs 6).length
   DEBUG:因子对:1,6
   DEBUG:因子对:2,3
   2
 

解决方案

我测试了对PowerShell的V2 CTP版的Windows XP,并且看到了同样的结果作为OP。

这个问题似乎是扁平化的集合PowerShell中的习惯,因为他们沿着管道传递。一个简单的解决方案是通过$ P $包裹返回值的集合pfacing前pression与这样逗号运算符返回:

 返回,$ factorPairs
 

请参阅基思·希尔的博客文章<一个href="http://rkeithhill.word$p$pss.com/2007/09/24/effective-powershell-item-8-output-cardinality-scalars-collections-and-empty-sets-oh-my/"相对=nofollow>有效的PowerShell第8项:!输出基数 - 标量,集合和空集 - 噢,我的了解更多的细节

希望这有助于。

I have the following PowerShell function that works well for any input except for 1. If I pass it an input of 1 it will return an array with two elements 1,1 instead of a single element which is itself an array of two elements (1,1).

Any ideas how I can make PowerShell return a jagged array with one element that is itself an array?

function getFactorPairs {
    param($n)
    $factorPairs = @()
    $maxDiv = [math]::sqrt($n)
    write-verbose "Max Divisor: $maxDiv"
    for($c = 1; $c -le $maxDiv; $c ++) {
        $o = $n / $c;
        if($o -eq [math]::floor($o)) {
            write-debug "Factor Pair: $c, $o"
            $factorPairs += ,@($c,$o) # comma tells powershell to add defined array as element in existing array instead of adding array elements to existing array
        }
    }
    return $factorPairs
}

Here is my test and it's output showing the problem. You can see that the first example (1 as input) is returning a length of 2 even though there was only one factor pair found. The second example (6 as input) is working properly and is returning a length of 2 with two factor pairs found.

~» (getFactorPairs 1).length  
   DEBUG: Factor Pair: 1, 1  
   2  

~» (getFactorPairs 6).length  
   DEBUG: Factor Pair: 1, 6  
   DEBUG: Factor Pair: 2, 3  
   2

解决方案

I tested this on the PowerShell V2 CTP on Windows XP, and saw the same results as the OP.

The problem appears to be PowerShell's habit of "flattening" collections as they're passed along the pipeline. A simple solution is to wrap the return value in an collection by prefacing the expression to be returned with the comma operator thus:

return ,$factorPairs

See Keith Hill's blog entry Effective PowerShell Item 8: Output Cardinality - Scalars, Collections and Empty Sets - Oh My! for a little more detail.

Hope this helps.

这篇关于铁血PowerShell的阵列正在失去一个维度只有一个元素存在,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆