- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,首先我认为自己是一个新手,关于 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-Content
和 Env 将间接技术应用于环境变量:
驱动器。
至于你尝试过的:
$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/
我正在实现一个算法,当用户输入字符串时,字符串中的每个字符(如果是字母表)都应该增加给定的值(这里是旋转器)。我正在玩这个代码 2 小时,但无法弄清楚为什么当我按值旋转器递增时,它会按 rotator
我有 1.0.5。我怎样才能增加到 1.0.6? 试过了,但是不行。 echo 1.0.5 0.0.1 | awk '{sum=$1+$2; printf"%0.2f\n", sum }' 最佳答案
这个问题在这里已经有了答案: Behaviour of increment and decrement operators in Python (11 个回答) Why are there no ++
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在尝试温习我的 C,我有以下代码,当我使用 i 偏移量但不使用 Hold++ 时,它可以工作,我不明白为什么我认为他们做了同样的事情?这是我无法开始工作的版本: char* reversestri
我需要增加/减少 PostgreSQL 数据库中的计时。 下面是包含列类型为"timestamp without time zone" 的表的输出 如果时间超过 24 小时,我也需要更改日期。请协助
我有一个名为 temp_rfm 的表,其中 col1 实际上是客户 ID(我有一个非法的联盟混合问题)和 calc_date 是增加月份的开始。 SELECT * FROM temp_rfm ; co
我目前正在处理我的应用程序的首选项,我必须设置一个角度。默认值约为 30°,用户应该能够调整此角度以使其最适合。 我不只是制作一个普通的 EditTextPreference,而是希望它可以像在其他应
这个问题已经有答案了: Increment a number by prefix and postfix operator (1 个回答) 已关闭去年。 我正在努力理解 Javascript 增量运算
我使用下面的 javascript 递归地重新加载一个目标 DIV,其 id="outPut",将参数传递给 getData.php 时执行数据查询的结果>。问题是 fadeTo 会淡化每次迭代调用返
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Jquery Draggable + Bring to Front 我有一个网站,用户可以在其中打开多个聊天
我必须定义一个函数,其中: Starting with a positive integer original, keep multiplying original by n and calculat
我正在我的应用程序中记录一些统计数据。其中一项统计数据是 BigDataStructure 的大小。我有两个选择: 创建一个计数器并递增/每次递减计数器有一个添加/删除大数据结构。 每次添加/删除从
在下面的 Java 示例程序中,我得到了无限循环,我不明白为什么: public class Time { public static int next(int v) { re
我从 C#/WPF 添加了一个意外的行为 private void ButtonUp_Click(object sender, RoutedEventArgs e) {
我想在 Python 2.7 中增加用户提供的字符串的最后一位数字。 我可以这样替换第一个数字: def increment_hostname(name): try: numb
我正在用蛮力搜索具有某些属性的 float (sin(a)^2+cos(a)^2-1 的小舍入误差)。因此,我想通过递增尾数来遍历给定 float 的邻域。 在 C 中是否有一种简单的方法可以做到这一
C 标准将 _Bool 定义为包含 0 或 1 的无符号类型。如果 _Bool 类型的值 1 递增,据我所知,有两个选项: 该值在 1 到 0 之间环绕 该值增加到 2,它是非零值,因此在转换回 _B
我有一个 INI 文件,其中存储了一些用于设置的整数。部分名称存储如下: [ColorScheme_2] name=Dark Purple Gradient BackgroundColor=224 B
我的应用程序中有这个方法: - (void)initializeTimer{ self.myTimer = [NSTimer scheduledTimerWithTimeInterval:th
我是一名优秀的程序员,十分优秀!