xHarbour Reference Documentation > Function Reference |
Provides a functional equivalent for the DO CASE statement.
HB_Decode( <xInitVal>, <xCase1>, <xRet1> , ; [<xCaseN>, <xRetN>], ; [<xDefault>] ) --> xReturn or HB_Decode( <xInitVal>, <hHash>, [<xDefault>] ) --> xReturn
The function returns the value <xRet> when <xInitValue> matches <xCase>. If no match is found, <xDefault>, if specified, is returned. Otherwise, the return value is NIL.
HB_Decode() can be viewed as a functional equivalent of to DO CASE statement. An initial value <xInitVal> is passed and compared to a list of <xCase> values. When a match is found, the corresponding <xRet> parameter is returned. <xCase> and <xRet> can be specified pairwise or may be combined in a single hash.
See also: | DO CASE, Hash(), HB_DecodeOrEmpty() |
Category: | Conversion functions , xHarbour extensions |
Source: | rtl\decode.prg |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example illustrates possibilities of using HB_Decode() PROCEDURE Main LOCAL nChoice, hHash := {=>} nChoice := 2 ? HB_Decode( nChoice, 1, "COM1", 2, "COM2", 3, "COM3", "NUL" ) // result: COM2 nChoice := 4 ? HB_Decode( nChoice, 1, "COM1", 2, "COM2", 3, "COM3", "NUL" ) // result: NUL hHash := { 0 => "Yesterday", ; 1 => "Today" , ; 2 => "Tomorrow" } nChoice := 0 ? HB_Decode( nChoice, hHash, "Never" ) // result: Yesterday nChoice := 3 ? HB_Decode( nChoice, hHash, "Never" ) // result: Never RETURN
http://www.xHarbour.com