LoginSignup
1
0

More than 5 years have passed since last update.

秀マクロ用Splitを作ってみた。

Posted at

最初にコード公開

Split:
    ##i=0;
    $$str=$$1;
    $$fnd=$$2;
    while(strstr($$str,$$fnd)>-1){
        $Spl[##i]=midstr($$str,0,strstr($$str,$$fnd));
        $$str=rightstr($$str,strlen($$str)-strstr($$str,$$fnd)-strlen($$fnd));
        ##i = ##i + 1;
    }
    $Spl[##i]=$$str;
    return ##i+1;

秀マクロのサブルーチンで再現してます。

引数は2つ。

  • 第1引数にSplit対象の文字列
  • 第2引数にSplit文字列

を指定します。

サブルーチンの中で

  • 第1引数は$$1(数値型の場合は##1
  • 第2引数は$$2(数値型の場合は##2

として利用できます。

コード説明

$$1$$2だと分かりづらいので、

  • $$str
  • $$fnd

というローカル変数に代入しています。

その後の処理の流れは以下の通り。

  • $$str内の文字列から$$fnd内の文字列を検索
  • 【見つかった場合】
    • その前までの文字列を配列$Splに格納
    • $$strの中からも抜き出す
    • $$fndの文字列も抜き出す
    • 繰り返し処理(1に戻る)
  • 【見つからなかった場合】
    • 繰り返し処理を終了
  • 残りの文字列を配列$Splに格納
  • 分割された数を関数の戻り値として返す

という感じ。

$Splはグローバル変数なので、
サブルーチンの外でも使えます。

利用例

// 区切り文字
$delim = "===";

// 置換文字セットリスト
$Rep[0] = "[  ¥¥t]+===, ";
$Rep[1] = "([a-zA-Z0-9]+)([^;])===¥¥1; ¥¥2";

// 置換文字セット数
#Rep = 2;

// メイン処理
#i = 0;
while(#i < #Rep){
    Split $Rep[#i], $delim;
    replaceallfast Spl[0],Spl[1],regular;
    #i = #i + 1;
}
endmacro;

// Split関数
Split:
    ##i = 0;
    $$str = $$1;
    $$fnd = $$2;
    while(strstr($$str,$$fnd) > -1){
        $Spl[##i] = midstr($$str,0,strstr($$str,$$fnd));
        $$str = rightstr($$str,strlen($$str) - strstr($$str,$$fnd) - strlen($$fnd));
        ##i = ##i + 1;
    }
    $Spl[##i] = $$str;
    return ##i + 1;

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0