文字列を指定の区切り文字で分割して、それぞれに対して繰り返し処理
Loop, Parse, InputVar [, Delimiters, OmitChars, FutureUse]
| 引数名 | 説明 |
|---|---|
| Parse |
第一引数は「PARSE」にする。 変数は使えない。 |
| InputVar |
分割される文字列が格納された変数名。 「%Name%」のようにすれば、「Name」変数に格納された文字列が変数名として使用される。 |
| Delimiters |
区切り文字として使用したい文字を列挙する。 特定の「文字列」を区切りとしたい場合、StringReplaceで置換してから処理するとよい。 「CSV」とすると、CSV形式のデータとして処理される。「"first field",SecondField,"the word ""special"" is quoted literally",,"last field, has literal comma"」のようなデータをうまく処理できる。 省略時は、1バイトずつ処理される。 |
| OmitChars | 各フィールドの先頭と末尾から取り除きたい文字を列挙。 |
| FutureUse | 将来の拡張のために確保されている。今のところこの引数は無視される。 |
「A_LoopField」変数で切り出された文字列を参照できる。
InputVarの内容はループ開始前に退避されるので、ループ内でInputVarの変数の内容を変更しても影響は無い。
Sortコマンドを使用すれば、フィールドを文字コード順に並び替えてから処理することができる。
StringSplitでも文字列を分割できるが、Loop,PARSEのほうがパフォーマンスが高い。
その他の仕様は、通常のLoopと同様。
StringSplit, Loop, Break, Continue, Blocks, Sort, FileSetAttrib, FileSetTime
; Example #1:
Colors = red,green,blue
Loop, parse, Colors, `,
{
MsgBox, Color number %a_index% is %A_LoopField%.
}
; Example #2: Read the lines inside a variable, one by one (similar to a file-reading loop).
; A file can be loaded into a variable via FileRead:
Loop, parse, FileContents, `n, `r
{
MsgBox, 4, , Line number %a_index% is %A_LoopField%.`n`nContinue?
IfMsgBox, No, break
}
; Example #3: This is the same as the example above except that it's for the clipboard.
; It's useful whenever the clipboard contains files, such as those copied from an open
; Explorer window (the program automatically converts such files to their file names):
Loop, parse, clipboard, `n, `r
{
MsgBox, 4, , File number %a_index% is %A_LoopField%.`n`nContinue?
IfMsgBox, No, break
}
; Example #4: Parse a comma separated value (CSV) file:
Loop, read, C:\Database Export.csv
{
LineNumber = %A_Index%
Loop, parse, A_LoopReadLine, CSV
{
MsgBox, 4, , Field %LineNumber%-%A_Index% is:`n%A_LoopField%`n`nContinue?
IfMsgBox, NO, return
}
}