Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Removed translated content for 'zh'
Sv translation
languageen

You can use if and case commands for implementing conditions and branching in transformation scripts.

If command

The if command is the most basic of all the control flow statements. It tells script to execute a certain section of code only if a particular test (condition) evaluates to true. Condition can be any expression containing boolean, integer and strings logic. Integer expression is evaluated to true when greather than 0 and string expression is evaluated to true when returns non blank value. It is recommended to use boolean expressions to maintain script readability.
Example of basic if command usege:

Code Block
val = value() as double
result = "Success"
if (val > 100) result = "Too high"
return result

The result value is set to "Too high" only if source value of the row is greater than 100. Otherwise the result in row after transformation is "Success".
You can use multiple commands after if command when you use brackets:

Code Block
val = value() as double
result = "Success"
if (val > 100) {
   result = "Too high"
   result += " (" + val.toString() + ")"
}
return result;

If comman can contain an else part to provide a secondary path of execution when an "if" clause evaluates to false. We can rewrite our first example as:

Code Block
val = value() as double
if (val > 100) {
    result = "Too high"
} else {
    result = "Success"
}
return result;

In case of simple conditions you can also use a "?, :" if notation for branching single commands:

Code Block
val = value() as double;
return val > 100 ? "Too high" : "Success";

Switch command

Unlike if and if else commands, the switch statement allows for any number of possible execution paths. You can use more sophisticated conditions. Following example ilustrates how to evaluate value x three different ways:

  1. Equals a specified string value
  2. Is one of the values from list
  3. Is number whithin an range
Code Block
switch (x) {
    case "Specific string value":
         result = "Contains specified string value"
         break
    case [4, 5, 'a', 'b']:
         result = "Is 4, 5, a, or b."
         break
    case 12..30:
         result = "In range"
         break
    case Number:
         result = "Is number"
         break
    default:
         result = "Default"
}

Another point of interest is the break statement after each case. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, case statements fall through; that is, without an explicit break, control will flow sequentially through subsequent case statements.

Technically, the final break is not required because flow would fall out of the switch statement anyway. However, we recommend using a break so that modifying the code is easier and less error-prone. The default section handles all values that aren't explicitly handled by one of the case sections.

Deciding whether to use if command or a switch statement is sometimes a judgment call. You can decide which one to use based on readability and other factors. If you have more than 2 ways branching, use switch command.
You can naturally nest the if and switch commands.

Samples

Return text according to value
Code Block
hodnota = value() as double;
result = "Lower than 100";
if (hodnota > 100) result = "Greater than 100";
return result;
Returns a negative or positive value of a column 5 according to text value in column 6
Code Block
if (value(6) == 'Credit') { return value(5)
} else if (value(6) == 'Debit') { return -value(5)
} else { return 0
}

For details about accessing other columns in script, see [Transformations over multiple columns].

Sv translation
languageja

条件を実装し、変換スクリプトに場合は、if とcase コマンドを使用できます。

ifコマンド 

ifコマンドは、すべての制御フロー計算書の最も基本的なものです。これは、特定のテスト(条件)がtrueと評価された場合のみに、コードの特定のセクションを実行するスクリプトを指示します。条件はboolean、整数や文字列のロジックを含む任意の式を指定できます。0より大きいのときに整数式の値をtrueに評価され、非ブランク値を返却するときに文字列式はtrueに評価されます。スクリプトの読みやすさを維持するためにブール式を使用することをお勧めします。

基本的なコマンドの使用方法の例

Code Block
val = value() as double
result = "Success"
if (val > 100) result = "Too high"
return result

結果値は、行のソース値が100より大きい場合にのみ、「高すぎ」に設定されます。それ以外の場合、変換後の行の結果は「成功」になります

ブラケットを使用するときは、ifコマンドの後に複数のコマンドを使用できます。

Code Block
val = value() as double
result = "Success"
if (val > 100) {
   result = "Too high"
   result += " (" + val.toString() + ")"
}
return result;

If コマンドがfalse と評価される時の二次パスを提供するためにIfコマンドがelseの一部を含めます。

私たちは、最初の例を次のように書き換えることができます。

Code Block
val = value() as double
if (val > 100) {
    result = "Too high"
} else {
    result = "Success"
}
return result;

単純な条件の場合、単一のコマンドを分岐するために"?, :"表記法を使用できます。

Code Block
val = value() as double;
return val > 100 ? "Too high" : "Success";

switchコマンド 

If及び他のコマンドと異なり、switchステートメントは、可能な実行パスの任意の数を可能にします。あなたはより洗練された条件を使用できます。以下の例に、3つの異なる方法でxの値を評価する方法を紹介します:

  1. 特定の文字列値に等しい

  2. リストからの値の1つ

  3. 番号が範囲内にある

Code Block
switch (x) {
    case "Specific string value":
         result = "Contains specified string value"
         break
    case [4, 5, 'a', 'b']:
         result = "Is 4, 5, a, or b."
         break
    case 12..30:
         result = "In range"
         break
    case Number:
         result = "Is number"
         break
    default:
         result = "Default"
}

関心のもう一つのポイントは、それぞれのケースの後のbreakステートメントです。 各break文は、囲みswitch文を終了します。 制御フローは、スイッチブロックの後の最初の文で継続されます。 breakステートメントが必要なのは、ステートメントがなければcaseステートメントが失敗するからです。 つまり、明示的に中断することなく、制御は後続のcase文を順番に流れます。

流れがとにかくswitchステートメントの外に落ちるので、技術的には、最終的な休憩が必要とされていません。しかし、コードを変更は簡単かつエラーが少なくなるようにブレークを使用してお勧めします。しかし、デフォルトのセクションは、明示的にケースのセクションのいずれかによって処理されないすべての値を処理します。

コマンドまたはswitchステートメントを使用するかどうかを決定することは時々判決コールである。あなたは読みやすさやその他の要因に基づいて使用するかを決定できます。以上の2通りの方法が分岐している場合、スイッチのコマンドを使用します。

ifコマンド及びswitchコマンドの場合に自然に切り替えることができます。

サンプル  

値に応じてテキストを返却

Code Block
hodnota = value() as double;
result = "Lower than 100";
if (hodnota > 100) result = "Greater than 100";
return result;

6カラム内のテキスト値に応じて、5カラムの負または正の値を返却します。

Code Block
if (value(6) == 'Credit') { return value(5)
} else if (value(6) == 'Debit') { return -value(5)
} else { return 0
}

スクリプトでの他のカラムへのアクセスの詳細については、「複数の列の上に変換」を参照してください。