LoginSignup
2
4

More than 1 year has passed since last update.

【Teratermマクロ】 if文で文字列が一致しているか判定

Posted at

Teratermマクロでinputboxの入力値に応じて、条件分岐をする仕組みを作ろうとしたところ、
比較演算子で判定できないという想定外。自分なりに考えた方法を残しておく。

strcompareを使う

;●インプット
inputbox '対象を入力してください。(3文字以上)' 'Input'
msg = inputstr
;●入力文字列のチェック
messagebox msg 'Input'

;●頭3文字の切り出し
strcopy msg 1 3 Re
;●頭3文字のチェック
messagebox Re 'test'

;●'abc'と同値か比較、結果をresultへ
strcompare Re 'abc'

;●判定、比較の繰り返し
if result = 0 then
    VALUE = 'ABC'
    goto output
else
    strcompare Re 'def'
    if result = 0 then
        VALUE = 'DEF'
        goto output
    else
        strcompare Re 'ghi'

        if result = 0 then
            VALUE = 'GHI'
            goto output         
        else
            VALUE = 'XXX'

        endif
    endif
endif

;結果----------------------------
:output
messagebox VALUE 'Message'
end

→比較結果がシステム変数resultに格納される為、
入れ子構造になってしまいコードが見づらくなった。
書き方を工夫すればもう少し見やすくはなりそう。

別案

str2codeを使って文字列をASCIIコードに数値変換。

inputbox '対象を入力してください。(3文字以上)' 'Input'
msg = inputstr
messagebox msg 'Input'

strcopy msg 1 3 Re
messagebox Re 'test'

str2code returncode Re

;●ASCIIコードの確認
;messagebox returncode 'test'

if returncode = 6382179(ASCIIコード) then
    VALUE = 'ABC'
    goto output
elseif returncode = (ASCIIコード) then
    VALUE = 'DEF'
    goto output
elseif returncode = (ASCIIコード) then
    VALUE = 'GHI'
    goto output         
else
    VALUE = 'XXX'
endif

:output
messagebox VALUE 'Message'
end

→if文の入れ子にする必要がないので、比較的シンプルに書ける。
問題はASCIIコードを確認する手間がかかる事

2
4
2

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
2
4