Loop,Parse

提供: AutoHotkey Wiki
移動: 案内検索

実行制御 | GUI表示 | 演算・変数 | メモリ・DLL操作 | 文字列操作 | キーボード | マウス | シェル | ウィンドウ | ウィンドウグループ
ステータスバー | コントロール | サウンド | ファイル | INIファイル | レジストリ | 環境変数 | AutoHotkey | その他 | 設定関係 | オブジェクト

Loop,Parse[編集]

文字列を指定の区切り文字で分割して、それぞれに対して繰り返し処理を行う。
Parsing-Loop、文字列分割ループとも言う。

Loop, Parse, InputVar [, Delimiters, OmitChars, FutureUse]

Parameters[編集]

引数名 説明
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 将来の拡張のために確保されている。今のところこの引数は無視される。

Remarks[編集]

A_LoopField変数で切り出された文字列を参照できる。

InputVarの内容はループ開始前に退避されるので、ループ内でInputVarの変数の内容を変更しても影響は無い。

Sortコマンドを使用すれば、フィールドを文字コード順に並び替えてから処理することができる。

StringSplitでも文字列を分割できるが、Loop,Parseのほうがパフォーマンスが高い。

その他の仕様は、通常のLoopと同様。

AHKL ユニコード版で区切り文字を省略した場合は、全角半角に関係なく見た目通りの「文字」毎にループが回る。
詳しくはAutoHotkey_Lの互換性を参照のこと。

[重要] 本家の公式版およびAHKLのANSI版では 日本語を扱う上での不具合 が起きうるので注意する。

Related[編集]

StringSplit, Loop, Break, Continue, Blocks, Sort, FileSetAttrib, FileSetTime

Example(s)[編集]

; 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
  }
}