Page History
Sv translation | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||
You can use if and case commands for implementing conditions and branching in transformation scripts. If commandThe 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.
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".
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:
In case of simple conditions you can also use a "?, :" if notation for branching single commands:
Switch commandUnlike 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:
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. SamplesReturn text according to value
Returns a negative or positive value of a column 5 according to text value in column 6
For details about accessing other columns in script, see [Transformations over multiple columns]. |
Sv translation | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||
条件を実装し、変換スクリプトに場合は、if とcase コマンドを使用できます。 ifコマンドifコマンドは、すべての制御フロー計算書の最も基本的なものです。これは、特定のテスト(条件)がtrueと評価された場合のみに、コードの特定のセクションを実行するスクリプトを指示します。条件はboolean、整数や文字列のロジックを含む任意の式を指定できます。0より大きいのときに整数式の値をtrueに評価され、非ブランク値を返却するときに文字列式はtrueに評価されます。スクリプトの読みやすさを維持するためにブール式を使用することをお勧めします。 基本的なコマンドの使用方法の例
結果値は、行のソース値が100より大きい場合にのみ、「高すぎ」に設定されます。それ以外の場合、変換後の行の結果は「成功」になります。 ブラケットを使用するときは、ifコマンドの後に複数のコマンドを使用できます。
If コマンドがfalse と評価される時の二次パスを提供するためにIfコマンドがelseの一部を含めます。 私たちは、最初の例を次のように書き換えることができます。
単純な条件の場合、単一のコマンドを分岐するために"?, :"表記法を使用できます。
switchコマンドIf及び他のコマンドと異なり、switchステートメントは、可能な実行パスの任意の数を可能にします。あなたはより洗練された条件を使用できます。以下の例に、3つの異なる方法でxの値を評価する方法を紹介します:
関心のもう一つのポイントは、それぞれのケースの後のbreakステートメントです。 各break文は、囲みswitch文を終了します。 制御フローは、スイッチブロックの後の最初の文で継続されます。 breakステートメントが必要なのは、ステートメントがなければcaseステートメントが失敗するからです。 つまり、明示的に中断することなく、制御は後続のcase文を順番に流れます。 流れがとにかくswitchステートメントの外に落ちるので、技術的には、最終的な休憩が必要とされていません。しかし、コードを変更は簡単かつエラーが少なくなるようにブレークを使用してお勧めします。しかし、デフォルトのセクションは、明示的にケースのセクションのいずれかによって処理されないすべての値を処理します。 コマンドまたはswitchステートメントを使用するかどうかを決定することは時々判決コールである。あなたは読みやすさやその他の要因に基づいて使用するかを決定できます。以上の2通りの方法が分岐している場合、スイッチのコマンドを使用します。 ifコマンド及びswitchコマンドの場合に自然に切り替えることができます。 サンプル値に応じてテキストを返却
6カラム内のテキスト値に応じて、5カラムの負または正の値を返却します。
スクリプトでの他のカラムへのアクセスの詳細については、「複数の列の上に変換」を参照してください。 |