【问题标题】:Powershell string to 2d string array- not char arrayPowershell字符串到二维字符串数组-不是char数组
【发布时间】:2021-09-17 14:13:32
【问题描述】:

更新:我正在尝试将用户输入值(字符串)放入二维数组。出于测试目的,我为变量分配了一个字符串。 目标:稍后我希望我的程序查看数组第 1 列的每一行,如果显示打印机,则执行安装打印机软件,如果没有打印机则不安装软件。我可以为一维数组执行此操作,但我想要 1 到 10 行。这就是为什么二维数组的想法会很复杂。

我想学习这样做,而不是要求你编写我所有的代码。 谢谢。

这是我的代码。

#set user input to a string variable
$UserInputVar = "computer1,noPrinter,Computer2,Printer,Computer3,Printer"

#this is my 2d array; Declare. i want to be able to use anywhere from 1 to 10 rows and always 2 columns
$my2d=[object[]]::(,2)

$my2d.clear #for testing purpose

#assign values of sting to 2 dimensional array
#split the string at each comma(,)
$my2d = $UserInputVar.Split(",")

#show me the values in this array
"`n #my2d[0][0] "
$my2d[0][0]   #expect value 'computer1'
$my2d[0][1]   #expect value 'noPrinter'
$my2d[0][2] 
$my2d[0][3]


"`n #my2d[1][0] "
$my2d[1][0]   #expect value 'computer2'
$my2d[1][1]   #expect value 'Printer'
$my2d[1][2] 
$my2d[1][3]


"`n #my2d[2][0] "
$my2d[2][0]   #expect value 'computer3'
$my2d[2][1]   #expect value 'Printer'
$my2d[2][2] 
$my2d[2][3]


"`n #array with no 2nd index"
$my2d[0]
$my2d[1]
$my2d[2]

【问题讨论】:

  • 该表不是字符串。 [grin] 请把它改成一个字符串……然后用代码标记把它换成纯文本格式。
  • $my2d = $UserInputVar.Split(",") 会给你一个一维数组。另外,这个问题到底是什么,我很困惑。
  • 我在示例中看到第 0 列有水果类型,第 1 列有价格。我想将用户输入作为字符串并将其分成 my2d[0][0] 和 my2d[0][1] 和 my2d[1][0]...等等。我不想要初始化值,但这就是我能找到的所有示例。
  • 对不起 Lee_Dailey。我把桌子放在顶部只是为了视觉目的。我改变了它,我认为我搞砸了。

标签: arrays string powershell multidimensional-array


【解决方案1】:

我不确定你是否想要一个对象作为输出,但如果是这样,这是一种简单的方法:

$z = 0
$UserInputVar = "This,is,my,strg,to,test".Split(',')

for($z=0; $z -lt $UserInputVar.Count; $z=$z+2)
{
    [pscustomobject]@{
        1 = $UserInputVar[$z]
        2 = $UserInputVar[$z+1]
    }
}

输出

1    2   
-    -   
This is  
my   strg
to   test

编辑

再看问题,这可能就是你要找的:

$z = 0
$UserInputVar = "This,is,my,strg,to,test".Split(',')
$result = [System.Collections.Generic.List[object]]::new()
for($z=0; $z -lt $UserInputVar.Count; $z=$z+2)
{
    $result.Add(@($UserInputVar[$z],$UserInputVar[$z+1]))
}

一些测试

PS /> $result[0]
This
is

PS /> $result[0][0]
This

PS /> $result[0][1]
is

PS /> $result[0][2]

PS /> $result[1][0]
my

【讨论】:

  • 我会再次阅读对象。输出看起来像我想要的。我不知道哈希表,但这也是一个考虑因素。这是我想出的第一个代码块。在宏伟的计划中,我需要关联而不是对象属性。更简单。让我更新我的问题和字符串。
  • 是的,我喜欢你所拥有的。我只有 3 周的时间进入 powershell 并在我写作时学习。 Buddy 为我的项目建议了二维数组,然后去度假。
  • 我正在运行版本=4.0.0.0。我收到错误方法调用失败,因为 [System.Collections.Generic.List`1[...]] 不包含名为“新”的方法。我以前用 ::new();我试图从我的原始代码中删除它,但没有成功。
  • 我将 new() 更改为 @() 并且我所有的错误都消失了,您的代码运行良好。你的新版本是完美的。谢谢@Santiago Squarzon
  • 对不起。这是整行“$result = [System.Collections.Generic.List[object]]@()”
【解决方案2】:

您还可以使用正则表达式匹配来返回包含 2 个元素的组。然后,您可以将每个返回的对拆分为一个数组(因为一元运算符,)。然后将所有返回的单个数组自动包装到Object[] 数组中。

$UserInputVar = "computer1,noPrinter,Computer2,Printer,Computer3,Printer"
$my2d = [regex]::Matches($UserInputVar,'[^,]+(,[^,]+|$)') |
    Foreach-Object {,($_.Value -split ',')}

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2016-05-06
    • 2020-09-02
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多