LUA プロシジャ
例6: String関数の使用
| 要素: |
PROC LUAステートメント SUBMITステートメントとENDSUBMITステートメント STRING.ENDS_WITH関数 STRING.RESOLVE関数 STRING.SPLIT関数 STRING.STARTS_WITH関数 STRING.TRIM関数 |
詳細
この例では、STRING関数を使用してテキスト値を操作します。
プログラム
proc lua;
submit;
mystring = "@noun@ @verb@ the @object@"
newstring1 = string.resolve(mystring, {noun="Pigs",verb="eat",object="pie"})
newstring2 = string.resolve(mystring, {noun="Cars",verb="guzzle",
object="gasoline"})
print ("Original string: " .. mystring)
print ("New string 1: " .. newstring1)
print ("New string 2: " .. newstring2)
if string.starts_with(newstring2, "Cars") then
print("Could be about cars: " .. newstring2)
else
print("Not about cars: " .. newstring2)
end
if string.ends_with(newstring2, "pie") then
print ("I love pie!" .. newstring2)
else
print("Too bad. No pie: " .. newstring2)
end
mystring = " The fox ate the chicken. "
trimmedstring = string.trim(mystring)
print ("Original string between ***s: ***" .. mystring .. "***")
print ("Trimmed string between ***s: ***" .. trimmedstring .. "***")
t = string.split(mystring," ")
for element in pairs(t) do
print("Table index " .. element .. ", Value is: " .. t[element] .. "\n")
end
endsubmit;
run;
プログラムの説明
PROC LUAステートメントを実行して、Luaステートメント処理を開始します。PROC LUAステートメントを使用すると、SASセッションでLuaコードを呼び出すことができます。SUBMITステートメントは、Luaステートメントのブロックの先頭を特定します。
proc lua;
submit;
文字列変数を作成し、その中にさまざまな値を代入します。置換トークンの名詞、動詞、目的語を含む文字列Mystringを作成します。STRING.RESOLVE関数を使用して、新しい文字列、Newstring1およびNewstring2を生成します。元の文字列と新しい文字列をSASログに出力します。
mystring = "@noun@ @verb@ the @object@"
newstring1 = string.resolve(mystring, {noun="Pigs",verb="eat",object="pie"})
newstring2 = string.resolve(mystring, {noun="Cars",verb="guzzle",
object="gasoline"})
print ("Original string: " .. mystring)
print ("New string 1: " .. newstring1)
print ("New string 2: " .. newstring2)
文字列変数の最初と最後で一致するものを探します。STRING.STARTS_WITH関数を使用して、Newstring2がCarsで始まるかどうかを確認してから、適切なメッセージを出力します。次に、STRING.ENDS_WITH関数を使用して、Newstring2がpieで終わっているかどうかを確認し、適切なメッセージを出力します。
if string.starts_with(newstring2, "Cars") then
print("Could be about cars: " .. newstring2)
else
print("Not about cars: " .. newstring2)
end
if string.ends_with(newstring2, "pie") then
print ("I love pie!" .. newstring2)
else
print("Too bad. No pie: " .. newstring2)
end
文字列値から先頭と末尾の空白文字を削除します。Mystringに新しい値を割り当てます。STRING.TRIM関数を使用して、先頭と末尾の空白文字を削除します。比較のために、元の文字列とトリミングされた文字列を出力します。
mystring = " The fox ate the chicken. "
trimmedstring = string.trim(mystring)
print ("Original string between ***s: ***" .. mystring .. "***")
print ("Trimmed string between ***s: ***" .. trimmedstring .. "***")
文の文字列に単語のリストを作成します。STRING.SPLIT関数を使用して、文字列Mystringから個々の単語のリストを生成します。デフォルトでは、空白の値は結果のリストに含まれません。Mystringの単語のリストを出力します。
t = string.split(mystring," ")
for element in pairs(t) do
print("Table index " .. element .. ", Value is: " .. t[element] .. "\n")
end
Luaステートメントのブロックを終了し、LUAプロシジャを実行します。ENDSUBMITステートメントで、Luaステートメントのブロックを終了します。
endsubmit;
run;
出力: 文字列関数結果を表示するSASログ
STRING関数の結果
NOTE: Lua initialized.
Original string: @noun@ @verb@ the @object@
New string 1: Pigs eat the pie
New string 2: Cars guzzle the gasoline
Could be about cars: Cars guzzle the gasoline
Too bad. No pie: Cars guzzle the gasoline
Original string between ***s: *** The fox ate the chicken. ***
Trimmed string between ***s: ***The fox ate the chicken.***
Table index 1, Value is: The
Table index 2, Value is: fox
Table index 3, Value is: ate
Table index 4, Value is: the
Table index 5, Value is: chicken.
NOTE: PROCEDURE LUA used (Total process time):
real time 0.20 seconds
cpu time 0.06 seconds
最終更新: 2026年9月18日