我在win10-64位下使用Powershell。我正在“设置位置”功能下的-path参数中输入路径。我的路径变量是System.Array的属性,即“ $ list.name”。在一个循环中,我经常调用“。\ $ list.name [$ i]”作为路径,但失败了。我想正确地调用嵌套变量。
PS C:\Users\admin\Documents\Rainmeter\Skins> $list
label name link sort
----- ---- ---- ----
1 chrome C:\Program Files (x86)\Google\Chrome\Application\chrome.exe 1
1 matlab C:\Program Files\MATLAB\R2019a\bin\matlab.exe 3
PS C:\Users\admin\Documents\Rainmeter\Skins> $i=0
PS C:\Users\admin\Documents\Rainmeter\Skins> Set-Location -Path .\$list.name[$i]
Set-Location : could not find path “.\ .name[0]”,the path is not existed.
+ Set-Location -Path .\$list.name[0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\ .name[0]:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
Set-Location -Path ".\$list.name[$i]"
$file = $list.name[$i]
Set-Location -Path $file
我想在循环下正确调用-path参数中的嵌套变量,并知道错误的原因。
谢谢您的帮助。
答案 0 :(得分:0)
尝试将其包装在subexpression operator ($(...)
)中。 PowerShell将在替换字符串的之前中执行。例如:
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
echo "<p>Variable x outside function is: $x</p>";
//The global keyword is used to access the global variable that is located within the function.
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
?>
<?php
myTest();
echo $y; // outputs 15
答案 1 :(得分:0)
您无法将位置设置为文件,请使用Set-Location -Path (Split-Path -Path $list.link[$i] -Parent)
获取链接的父目录,而不是名称。
import re
def determine_grade(score):
if score == 4:
return 'A'
elif score >= 3.70:
return 'A-'
elif score >= 3.30:
return "B+"
elif score >= 3.00:
return "B"
elif score >= 2.70:
return "B-"
elif score >= 2.30:
return "C+"
elif score >= 2.00:
return "C"
elif score >= 1.70:
return "C-"
elif score >= 1.30:
return "D"
elif score > 0:
return "D-"
elif score == 0:
return "F"
with open("studentsGrade.txt", "r+") as fin:
with open("out.txt", "w+") as fout:
for line in fin:
numericGrade = re.findall(r'(?:\d+(?:\.\d+)?)', line)
alphaGrade = determine_grade(float(numericGrade[1]))
fout.write(line.replace(str(numericGrade[1]), alphaGrade))