• No results found

Compound operators

In document xHarbour Language Reference Guide (Page 54-68)

Operator Equivalent Inline operation

x += y x := (x + y) Concatenation, Addition

x -= y x := (x - y) Concatenation, Subtraction

x *= y x := (x * y) Multiplication

x /= y x := (x / y) Modulus

x ^= y x := (x ^ y) Exponentiation

Note that there is no **= compound assignment operator, but only the ^= operator.

= (compound assignment)

Info

See also: %,*,**,+,++,-,--,/,:=

Category: Assignment operators,Mathematical operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates the results of compound operators.

PROCEDURE Main

LOCAL cString := "A"

LOCAL dDate := StoD( "20051130" ) LOCAL nValue := 2

? cString += "B " // result: "AB "

? cString -= "C " // result: "ABC "

? dDate += 24 // result: 12/24/2005

? (nValue ^= 3) - 6 // result: 2.00

? nValue // result: 8.00

nValue ^= 4

? nValue // result: 4096.00

RETURN

==

==

Exact equal operator (binary): compares two values for identity.

Syntax

<Expression1> == <Expression2>

Arguments

<Expression>

The values of <Expression1> and <Expression2> must have the same data type or can be NIL, and are compared.

Description

The "exact equal" operator compares two values of the same data type or NIL. It returns .T. (true) when the left operand has exactly the same value as the right operand, otherwise the return value is .F.

(false).

The data types Character and Memo are compared only on the basis of the ASCII values of their characters. The SET EXACT setting is ignored. That means, two operands have exactly the same character values when both have the same length and contain the exact same sequence of characters (case sensitive).

The data types Date, Logic and Numeric are compared to be exactly equal when both operands have the same value.

Variables holding the complex data types Array, Code block, Hash and Object do not store a va lue but a reference to the value. The "exact equal" operator returns .T. (true) when both operands have the same reference to the complex data type.

Info

See also: $,<,<=,<> != #,= (comparison),>,>=

Category: Comparison operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates resultsof the exact equal operator.

PROCEDURE Main

? "ABC" == "ABC" // result: .T.

? "ABC" == "XYZ" // result: .F.

? "ABC" == "ABC " // result: .F.

aArray1 := {1, 2, 3} // Create array reference aArray2 := {1, 2, 3} // Create new array reference aArray3 := aArray1 // Assign array reference

? aArray1 == aArray2 // result: .F.

? aArray1 == aArray3 // result: .T.

? aArray2 == aArray3 // result: .F.

==

RETURN

>

>

Greater than operator (binary): compares the size of two values.

Syntax

<Expression1> > <Expression2>

Arguments

<Expression>

The values of <Expression1> and <Expression2> must have the same data type and are compared.

Description

The "greater than" operator compares two values of the same data type. It returns .T. (true) when the left operand is greater than the right operand, otherwise the return value is .F. (false).

Only the simple data types Character, Data, Logic, Memo and Numeric can be compared. The complex data types Array, Code block, Hash, Object and the value NIL cannot be used with the "greater than"

operator.

Comparison rules

Data type Comparison

Character The comparison is based on the ASCII value of the characters.

Date T he comparison is based on the underlying date value.

Logical False (.F.) is smaller than true (.T.).

Memo Same as Character data type.

Numeric Comparison is based on the value of the numerics.

SET EXACT

For character comparisons, the greater than operator takes the SET EXACT setting into account. With SET EXACT OFF, characters are compared up to the length of the right operand. With SET EXACT ON, characters are compared up to the length of the left operand and trailing blank spaces are ignored.

Info

See also: $,<,<=,<> != #,= (comparison),==,>=,SET EXACT Category: Comparison operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates the result of the ">" operator.

PROCEDURE Main

? "A" > "Z" // result: .F.

? "a" > "Z" // result: .T.

? CToD("12/28/2005") > CToD("12/31/2005") // result: .F.

? .T. > .F. // result: .T.

>

? 2 > 1 // result: .T.

SET EXACT OFF

? "ab" > "a" // result: .F.

SET EXACT ON

? "ab" > "a" // result: .T.

RETURN

>=

>=

Greater than or equal operator (binary): compares the size of two values.

Syntax

<Expression1> >= <Expression2>

Arguments

<Expression>

The values of <Expression1> and <Expression2> must have the same data type and are compared.

Description

The "greater than or equal" operator compares two values of the same data type. It returns .T. (true) when the left operand is greater than or equal to the right operand, otherwise the return value is .F.

(false).

Only the simple data types Character, Data, Logic, Memo and Numeric can be compared. The complex data types Array, Code block, Hash, Object and the value NIL cannot be used with the "greater than or equal" operator.

Comparison rules

Data type Comparison

Character The comparison is based on the ASCII value of the characters.

Date The comparison is based on the underlying date value.

Logical False (.F.) is smaller than true (.T.).

Memo Same as Character data type.

Numeric Comparison is based on the value of the numerics.

SET EXACT

For character comparisons, the "greater than or equal" operator takes the SET EXACT setting into account. With SET EXACT OFF, characters are compared up to the length of the right operand. With SET EXACT ON, characters are compared up to the length of the left operand and trailing blank spaces are ignored.

Info

See also: $,<,<=,<> != #,= (comparison),==,>,SET EXACT Category: Comparison operators,Operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example demonstrates the result of the ">=" operator.

PROCEDURE Main

? "A" >= "Z" // result: .F.

? "a" >= "Z" // result: .T.

? CToD("12/28/2005") >= CToD("12/31/2005")

>=

// result: .F.

? .T. >= .F. // result: .T.

? 2 >= 1 // result: .T.

SET EXACT OFF

? "a" >= "a " // result: .F.

? "a" >= "ab" // result: .F.

? "" >= "a" // result: .F.

? "a" >= "" // result: .T.

SET EXACT ON

? "a" >= "a " // result: .T.

? "a" >= "ab" // result: .F.

? "" >= "a" // result: .F.

? "a" >= "" // result: .T.

RETURN

>>

>>

Right-shift operator (binary): shifts bits to the right.

Syntax

<nNumber1> >> <nNumber2>

Arguments

<nNumber1>

<nNumber1> is a numeric value whose bits are shifted to the right.

<nNumber2>

<nNumber2> is a number indicating how many places the bits are shifted to the right.

Description

The >> operator accesses individual bits in the left operand and shifts them to the right as many times as specified with the right operand. Both operands are always treated as integer values. If an operand has a decimal fraction, it is truncated prior to the operation.

A shift operation involves all bits of the left operand. When the bits are shifted one place to the right, the lowest bit is discarded and the highest bit is set to 0. A numeric value has 32 bits on a 32 bit operating system.

Shifting a nonnegative value to the right is equivalent with dividing <nNumber1> by 2 raised to the power of <nNumber2>.

Info

See also: <<,HB_BitShift()

Category: Bitwise operators,Operators,xHarbour extensions

LIB: xhb.lib

DLL: xhbdll.dll

Example

// This example shifts the bits of the value 52 two places to // the right so the value becomes 13.

PROCEDURE Main

nValue := 52 // binary: 00110100

? nValue >> 2 // binary: 00001101 (value = 13)

? 52 / 2 ^ 2 // result: 13

RETURN

@

@

Pass-by-reference operator (unary): passes variables by reference.

Syntax

@ <Variable>

Arguments

<Variable>

<Variable> is the name of any valid memory variable. Field variables cannot be passed by reference.

Description

When a variable is passed by reference to a subroutine, any changes made to the variable are reflected in the calling routine once the called routine has returned.

Normally, variables are passed by value. This means that the called routine receives a copy of the passed value so that the variable's value in the calling routine remains unaffected by assignment operations in the called routine.

When a variable is prefixed with the @ operator in a function call, the called function receives a reference to the variable. The variable can then be assigned a new value in the called function. The changed value is reflected in the calling routine when the called function has finished.

Passing variables by reference is usually required when a subroutine must deliver more than one return value.

Info

See also: FUNCTION,METHOD (Declaration),PROCEDURE Category: Operators,Special operators

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example shows the difference between a function call with // and without the @ operator:

PROCEDURE Main()

LOCAL cMsg := "Hello World"

? GetMsg( cMsg ) // result: Ok

? cMsg // result: Hello World

? GetMsg( @cMsg ) // result: Ok

? cMsg // result: Hi there

RETURN NIL

FUNCTION GetMsg( cString ) cString := "Hi there"

RETURN "Ok"

@()

@()

Function-reference operator (unary): obtains a function pointer.

Syntax

( @<funcName>() ) --> pFuncPointer

Arguments

<funcName>

<funcName> is the symbolic name of a function or procedure to obtain the function pointer from. Note that the function-reference operator can only be used within parentheses. If this syntactical rule is not complied with, a syntax error is reported by the compiler.

Description

The function-reference operator retrieves the pointer to a function or procedure. The resulting value is of Valtype()=="P".

A function pointer can be used with functionHB_Exec()orHB_ExecFromArray()to execute the function indirectly. This type of indirect execution is similar to using <funcName> within a macro-expression, or embedding the call within a code block which is then passed to theEval()function. If a function is to be called indirectly for multiple times, however, the usage of the function-pointer along with HB_Exec() or HB_ExecFromArray() has a considerable performance advantage.

Note: The function-reference operator can resolve FUNCTIONs or PROCEDUREs declared as STATIC only if the referenced <funcName> is declared in the same PRG file where the operator is used. Pointers to methods of objects can only be obtained using functionHB_ObjMsgPtr().

Info

See also: {|| },( ),FUNCTION,HB_Exec(),HB_ExecFromArray(),HB_FuncPtr(), HB_ObjMsgPtr(),METHOD (Declaration),PROCEDURE

Category: Operators,Indirect execution,Special operators,xHarbour extensions

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example shows the difference of indirect execution using a // code block and a function pointer

PROCEDURE Main

LOCAL pFunc := ( @Test() )

LOCAL bBlock := {|x,y| Test( x, y ) }

? Valtype( bBlock ) // result: B

? Valtype( pFunc ) // result: P EVal( bBlock, "Hello", "World" ) HB_Exec( pFunc, NIL, "Hello", "World" ) RETURN

PROCEDURE Test( ... )

@() LOCAL xVal

? PCount()

FOR EACH xVal IN HB_AParams()

?? xVal NEXT RETURN

HAS

HAS

Searches a character string for a matching regular expression.

Syntax

<cString> HAS <cRegEx>

Arguments

<cString>

This is the character string being searched.

<cRegEx>

This is a character string holding the search pattern as a literal regular expression. Alternatively, the return value ofHB_RegExComp()can be used.

Description

The HAS operator searches the character string <cString> for a substring based on the regular expression <cRegExp>. If the string contains a substring that matches the search pattern, the result of the operator is .T. (true). When no match is found, the result is .F. (false).

If the HAS operator is applied often in a search routine with the same regular expression, a literal regular expression <cRegEx> can be compiled with HB_RexExComp() to speed up operations.

Info

See also: $,AScan(),IN,LIKE,HB_RegEx(),HB_RegExComp(),HScan()

Category: Character operators,Comparison operators,Operators,Regular expressions,xHarbour extensions

LIB: xhb.lib

DLL: xhbdll.dll

Example

// The example uses two regular expressions to detect if // a character string contains a number or a Hex number.

// Note that one regular expression is a literal string // while the second is compiled.

PROCEDURE Main

LOCAL aExpr := { "0xEA", "3.14", "aChar", DtoC( Date() ), "0x0d" } LOCAL cHex := HB_RegExComp( "0[xX][A-Fa-f0-9]+" )

LOCAL cString

FOR EACH cString IN aExpr

? cString, ""

IF cString HAS "[0-9].?[0-9]*"

IF cString HAS cHex

?? "contains a Hex number"

ELSE

?? "contains a number"

ENDIF

HAS ELSEIF cString HAS cHex

?? "contains a Hex number"

ELSE

?? "contains no number"

ENDIF NEXT

** Output:

// 0xEA contains a Hex number // 3.14 contains a number // aChar contains no number // 09/21/06 contains a number // 0x0d contains a Hex number RETURN

IN

IN

Searches a value in another value.

Syntax

<cSubString> IN <cString>

<xValue> IN <aArray>

<xKey> IN <hHash>

Arguments

<cSubString>

<cSubString> is a character or other value that is searched for in <cString>.

<aArray>

<aArray> is an array with random values.

<xKey>

<xKey> is a value to search in a hash.

<hHash>

<hHash> is a hash value whose keys are searched for <xKey>.

Description

The IN operator searches the left operand in the right operand and returns .T. (true) if the value of the left operand is contained in the value of the right operand, otherwise .F. (false) is returned.

In document xHarbour Language Reference Guide (Page 54-68)