Eval()
Evaluates a code block.
Syntax
Arguments
<bBlock>
A code block to be evaluated
<xValue>
Any number of values to be passed as arguments to the code block. Return
The function returns the value of the last expression inside the code block.
Description
This function evaluates the code block <bBlock> and passes on to it all parameters specified as the comma separated list <xValues,...>. A code block can contain multiple, comma separated expressions that are executed from left to right. The value of the last expression executed in the code block is used as return value of Eval().
Code blocks are values that contain executable code. Normally, the xHarbour compiler creates the code associated with code blocks at compile time. It is possible, however, to create code blocks at runtime of an application. This is accomplished by the macro operator (&) which compiles a character string holding the syntax for a code block at runtime.
Código: Selecionar todos
Example
// The example outlines basic rules for code block usage.
PROCEDURE Main
LOCAL bBlock
bBlock := {|x| QOut(x) }
Eval( bBlock, Date() ) // result: NIL (of QOut())
// displays: 02/08/06
bBlock := {|x,y| x + y }
? Eval( bBlock, 5, 17 ) // result: 22
? Eval( bBlock, "A", "string" ) // result: Astring
RETURN
--------------------------------------------------------------------------------
Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe
************************************************************************************************
AEval()
Evaluates a code block for each array element.
Syntax
Código: Selecionar todos
AEval( <aArray>, <bBlock>, [<nStart>], [<nCount>] ) --> aArray
Arguments
<aArray>
This is the array to iterate.
<bBlock>
The code block evaluated for the elements of <aArray>. It receives two parameters: the value of the current array element and its numeric position in the array.
<nStart>
This is a numeric expression indicating the first element in the array to begin with. It defaults to 1, the first element of <aArray>.
<nCount>
A numeric expression specifying the number of elements to pass to the code block. It defaults to 1+Len(<aArray>)-<nStart>. Return
The function returns a reference to <aArray>.
Description
The array function AEval() traverses an array beginning with the element <nStart> and passes the values of individual array elements to the code block <bBlock>. The code block is evaluated and AEval() proceeds to the next array element until either <nCount> elements are processed or the end of the array is reached. The return value of the code block is ignored.
With each iteration, the code block receives as parameters the value stored in the current array element and a numeric value indicating the ordinal position of the current array element.
Note that AEval() is appropriate for iterating an array once. Operations that require a more sophisticated iteration or that alter array elements are programmed in FOR...NEXT or FOR EACH loops.
Example
Código: Selecionar todos
// The first part of the example displays the contents of an array while the
// second part first increases the value of the elements and then displays it
// on the screen.
PROCEDURE Main()
LOCAL aArray := {"A", "B", "C", "D", "E", "F" }
? "Simple iteration: "
AEval( aArray, {|x| QQOut(x) } ) // result: ABCDEF
? "Fill array with consecutive numbers: "
AEval( aArray, {|x,n| aArray[n] := n } )
// Note: LOCAL variable is visible within code block
// since aArray and code block are created in the
// same procedure.
AEval( aArray, {|x| QQOut(LTrim(Str(x))) } )
// result: 123456
RETURN
--------------------------------------------------------------------------------
Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe