从PowerShell中的2个变量创建一个增量变量
好的,首先,我认为自己是新手,并且有很多了解PowerShell,这是我有史以来的第一篇文章。 我正在尝试循环浏览一些数据,并将其放入自定义对象中,并将它们放入单独的数组以供以后使用。问题是我想通过使用计数器$ i创建一个代表$ Week_data1的变量,以便可以减少所需的代码量。我确实有一个串联的变量正在写出:写入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 char:17
$ Week_data $ i += $ date_data#将CSV文件中的数据添加到一个
~~
表达式或语句中意外的令牌“ $ i”。 + categoryInfo:parserError:{:) [],parentContainsRorrecordException +完全QualififiedErrid:意外情况
OK, First I consider myself a newbie and have much to learn about PowerShell and this is my first post ever.
I am trying to loop through some data and put it into a custom object and put them into separate arrays for later use. The issue is that I want to create a variable representing $week_data1 by using a counter $i so I can reduce the amount of code required. I do have a concatenated variable being written out: write-host '$week++ ='$week$i But I think it is being represented as a string?
How can I get $week_data$i to represent the array to insert the data?
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
}
}
}
}
Issue using $week_data$i as a variable I get an error:
At line:38 char:17
$week_data$i += $date_data # Add data from csv file into a
~~
Unexpected token '$i' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找变量间接,即可以通过存储在另一个变量中或从表达式中返回的名称间接地引用变量的能力。
但是请注意,通常有优越的替代方案,例如使用阵列或 Hashtables 作为Multi -Value容器 - 请参见示例。
如果您确实需要使用可变间接方向,请使用 get-variable 和
set-variable
:作为旁边:“扩展”具有
+=
的数组很方便,但效率低下:a new 必须每次在场景后面创建 - 请参见此答案。同样,与直接分配和变量引用相比,调用 cmdlet 进行设置和获取变量的性能差。
请参阅此答案使用 emoverencome embocore 变量,使用
get-get-get-get-- content
/set-content
和env:
驱动器。至于您尝试过的:
$ week_data $ i = ...
是一个分配 expression ,它被解释为直接并列<< em>两个变量,$ Week_data
和$ i
,这会导致您看到的 andtax错误您看到的。相比之下,类似
书面输出$ Week_data $ i
是命令,而$ Week_data $ i
也被解释为两个变量引用,作为a 命令参数它在句法上有效,只需通过两个变量值的(stringified) conmanation ;换句话说:$ WEEK_DATA $ i
表现得好像是双重引用的,即AN a>,因此命令等效于写出“ $ Week_data $ i”
You're looking for variable indirection, i.e. the ability to refer to a variable indirectly, by a name stored in another variable or returned from an expression.
Note, however, that there are usually superior alternatives, such as using arrays or hashtables as multi-value containers - see this answer for an example.
If you do need to use variable indirection, use
Get-Variable
andSet-Variable
:As an aside: "extending" an array with
+=
is convenient, but inefficient: a new array must be created behind the scenes every time - see this answer.Similarly, calling cmdlets to set and get variables performs poorly compared to direct assignments and variable references.
See this answer for applying the indirection technique analogously to environment variables, using
Get-Content
/Set-Content
and theEnv:
drive.As for what you tried:
$week_data$i = ...
is an assignment expression, which is interpreted as directly juxtaposing two variables,$week_data
and$i
, which causes the syntax error you saw.By contrast, something like
Write-Output $week_data$i
is a command, and while$week_data$i
is also interpreted as two variable references, as a command argument it is syntactically valid, and would simply pass the (stringified) concatenation of the two variable values; in other words:$week_data$i
acts as if it were double-quoted, i.e. an expandable string, and the command is therefore equivalent toWrite-Output "$week_data$i"
与答案无关,但可能对您有所帮助,我的功能将确定给定日期一个月的哪一周。
因此,您可以将可以转换为类似日期的字符串喂食:
或者
您会收回一个指示当天月份(周六结束)的数字。
Unrelated to the answer, but likely helpful for you, I have a function that will determine what week in a month a given date is.
So you feed that a string that can be converted to a date like:
or
and you get back a number indicating what week (ending on Saturday) of the month that day falls in.