Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将一行中的两个数组写入文件Powershell_Arrays_String_Powershell_Integer_Addition - Fatal编程技术网

Arrays 将一行中的两个数组写入文件Powershell

Arrays 将一行中的两个数组写入文件Powershell,arrays,string,powershell,integer,addition,Arrays,String,Powershell,Integer,Addition,我总是能够通过搜索功能在堆栈溢出上找到我一直在寻找的内容。然而,这次我有点卡住了,尽管我会突然问社区 我试图使用两个数组中的数据写入文件,例如: $arrayString = @(“User1″, “User2″, “User3″) $arrayInteger = @(1, 2, 3) 我不确定的那一行是将其写入文件的那一行,我尝试了以下操作: Add-Content $filePath$ ($arrayString[0] $arrayinteger[0]) 执行此操作时,在表达式或语句中出

我总是能够通过搜索功能在堆栈溢出上找到我一直在寻找的内容。然而,这次我有点卡住了,尽管我会突然问社区

我试图使用两个数组中的数据写入文件,例如:

$arrayString = @(“User1″, “User2″, “User3″)
$arrayInteger = @(1, 2, 3)
我不确定的那一行是将其写入文件的那一行,我尝试了以下操作:

Add-Content $filePath$ ($arrayString[0] $arrayinteger[0])
执行此操作时,在表达式或语句中出现错误“意外标记”arrayinteger

我也尝试过:

Add-Content $filePath$ ($arrayString[0] + $arrayinteger[0])
我得到的错误是“方法调用失败,因为[System.IO.DirectoryInfo]不包含名为“op_Addition”的方法”。我的理解是,我试图“添加”一个字符串和一个导致错误的整数

但是,如果我尝试将该行写入控制台,则可以:

write-host $arrayString[0] $arrayinteger[0]
我试图获得的目标输出是

User1 1
User2 2
User3 3

谢谢你的帮助。

这里有几种不同的方法来解决这个问题:

$arrayString = @("User1","User2","User3")
$arrayInteger = @(1, 2, 3)
在可展开字符串中使用子表达式:

$(for ($i=0; $i -le 2;$i++)
 {
   "$($arrayString[$i]) $($arrayInteger[$i])"
 })| set-content $filepath
使用格式字符串:

 $(for ($i=0; $i -le 2;$i++)
 {
   '{0} {1}' -f $arrayString[$i],$arrayInteger[$i] 
 }) | set-content $filepath

太好了!谢谢!我最终使用了子表达式方法,这是第一步。标记为已回答