There are two instances of Foreach in Powershell. One is a keyword, and the other is an alias for Foreach-object. The easiest way to loop through a csv file is to import-csv, send the result through the pipeline, and use Foreach-Object to introduce the loop. The special symbol "%" is also shorthand for Foreach-Object.
Try this:
Import-csv -path myfile.csv |
Foreach-object {
$sym = $_.symbol
$code = $_.'code word'
"The code word for $sym is $code"
}
On this csv file.
symbol,code word,morse code,phonetic
A,Alfa/Alpha,"● ?",AL FAH
B,Bravo,"? ● ● ●",BRAH VOH
C,Charlie,"? ● ? ●",CHAR LEE
D,Delta,"? ● ●",DELL TAH
E,Echo,"●",ECK OH
F,Foxtrot,"● ● ? ●",FOKS TROT
G,Golf,"? ? ●",GOLF
Z,Zulu,"? ? ? ? ?",ZOO LOO
H,Hotel,"● ● ● ●",HOH TELL
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…