gpt4 book ai didi

powershell - 在 PowerShell 中从 2 个变量创建一个递增变量

转载 作者:行者123 更新时间:2023-12-02 18:39:38 24 4
gpt4 key购买 nike

好的,首先我认为自己是一个新手,关于 PowerShell 还有很多东西需要学习,这是我的第一篇文章。我试图循环一些数据并将其放入自定义对象中,并将它们放入单独的数组中以供以后使用。问题是我想通过使用计数器 $i 创建一个表示 $week_data1 的变量,这样我就可以减少所需的代码量。我确实写出了一个串联变量: write-host '$week++ ='$week$i 但我认为它被表示为字符串?如何让 $week_data$i 代表要插入数据的数组?

Input data. Each week ends on Saturday.
$week1=@('2021-05-01')
$week2=@('2021-05-02', '2021-05-03', '2021-05-04', '2021-05-05', '2021-05-06', '2021-05-07', '2021-05-08')
$week3=@('2021-05-09', '2021-05-10', '2021-05-11', '2021-05-12', '2021-05-13', '2021-05-14', '2021-05-15')
$week4=@('2021-05-16', '2021-05-17', '2021-05-18', '2021-05-19', '2021-05-20', '2021-05-21', '2021-05-22')
$week5=@('2021-05-23', '2021-05-24', '2021-05-25', '2021-05-26', '2021-05-27', '2021-05-28', '2021-05-29')
$week6=@('2021-05-30', '2021-05-31')
$month =@($week1, $week2, $week3, $week4, $week5, $week6)

Create the output structures to be populated.
$week_data1=@()
$week_data2=@()
$week_data3=@()
$week_data4=@()
$week_data5=@()
$week_data6=@()
$month_data =@($week_data1, $week_data2, $week_data3, $week_data4, $week_data5, $week_data6)
Loop through the array and count the week number that is being processed.

$i = 0
foreach($week in $month)
{ $i++
$n=0

Here I can write out a Variable and it concatenates properly.
**write-host '$week++ ='$week$i**
foreach($day in $week)
{$n++
write-host '$day ='$day
Pull in data from a .csv file to populate the custom object.
foreach($line in $csv)
{
if($line -match $day)
Match the line in the CSV file that has the correct Date in it. One line in the file per date in the month.
{ #write-host '$line.Day = ' $line.Day
# custom object to be used later
$date_data = [PSCustomObject] @{
week_numb = $i
date = $line.Day
attempts = $line.Attempts
connects = $line.Connects
}
I have tried different syntax versions but it does not work here? I want to put the custom object data into the new array for the week being processed.
#write-host '$week_data[$i]='$week_data[$i]
$week_data$i += $date_data # Add data from csv file into a
#$week_data[$i] += $date_data
}
}

}
}

使用 $week_data$i 作为变量时出现错误:

行:38 字符:17

  •   $week_data$i += $date_data # Add data from csv file into a
  •             ~~

表达式或语句中出现意外标记“$i”。+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException+ FullQualifiedErrorId:UnexpectedToken

最佳答案

您正在寻找变量间接即通过存储在另一个变量中或从表达式返回的名称间接引用变量的能力。

但请注意,通常有更好的替代方案,例如使用数组或 hashtables 作为多值容器 - 请参阅 this answer 的示例。

如果您确实需要使用变量间接寻址,请使用 Get-Variable Set-Variable :

$week_data1 = 'foo', 'bar'

$i = 1

# Same as: $week_data1
# Note that "$" must NOT be specified as part of the name.
Get-Variable "week_data$i" -ValueOnly

# Same as: $week_data1 = 'baz', 'quux'
Set-Variable "week_data$i" baz, quux

# Updating an existing value requires nesting the two calls:
# Same as: $week_data1 += 'quuz'
Set-Variable "week_data$i" ((Get-Variable "week_data$i" -ValueOnly) + 'quuz')

顺便说一句:使用 += “扩展”数组很方便,但效率低下:每次都必须在幕后创建一个数组 - 请参阅 this answer

同样,与直接赋值和变量引用相比,调用 cmdlet 来设置和获取变量的性能较差。

请参阅 this answer,了解使用 Get-Content/Set-ContentEnv 将间接技术应用于环境变量:驱动器。


至于你尝试过的:

  • $week_data$i = ... 是一个赋值表达式,它被解释为直接并置两个变量,$week_data$i,这会导致您看到的语法错误

  • 相比之下,Write-Output $week_data$i 是一个命令,而 $week_data$i 是也解释为两个变量引用,作为命令参数,它在语法上是有效的,并且只会传递两个变量的(字符串化)串联值(value)观;换句话说: $week_data$i 的作用就好像它是双引号的,即 expandable string ,因此该命令相当于 Write-Output "$week_data$i"

关于powershell - 在 PowerShell 中从 2 个变量创建一个递增变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68213804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com