why thy Syntax does not work
- Code: Select all Expand view
- * AEval( aValues, {|x,i| x := FieldGet(i) } )
i have to use FOR NEXT loop
- Code: Select all Expand view
- iMax := LEN(aValues)
FOR ii := 1 TO iMax
aValues[ii] := FieldGet(ii)
NEXT
* AEval( aValues, {|x,i| x := FieldGet(i) } )
iMax := LEN(aValues)
FOR ii := 1 TO iMax
aValues[ii] := FieldGet(ii)
NEXT
https://harbour.github.io/doc/ Can you please explain how this works:
AEval( aValues, {|x,i| aValues[ i ] := FieldGet(i) } )
'
ChatGPT
The function you're asking about appears to be from Harbour, a language that's an open-source implementation of the Clipper programming language. Let's break it down:
AEval( aValues, {|x,i| aValues[ i ] := FieldGet(i) } )
Here, AEval() is a function that applies a block of code to each element in an array. The array in this case is aValues, and the block of code is {|x,i| aValues[ i ] := FieldGet(i) }.
The block of code {|x,i| aValues[ i ] := FieldGet(i) } is a lambda function or anonymous function, where x and i are the parameters. In the context of AEval(), x is the current element in the array aValues and i is the current index.
FieldGet(i) is a function that retrieves the value of a field from the current work area at position i. In this context, it's being used to set the ith element of aValues to the value of the ith field from the current work area. The := operator is an assignment operator, similar to = in other languages.
So, in plain English, this function is going through each element of the array aValues, and for each element, it's setting that element's value to the value of the corresponding field in the current work area.
Please note that the behavior of the FieldGet() function may depend on the specifics of the current work area and the fields it contains. Without more context, this is the most detailed explanation I can provide.
User
What is the differences to this code: :
'
iMax := LEN(aValues)
FOR ii := 1 TO iMax
aValues[ii] := FieldGet(ii)
NEXT
'
ChatGPT
The code you provided accomplishes the same task as the AEval() function in the previous example, but it does so in a more explicit manner using a FOR loop.
Here's what it does:
harbour
Copy code
iMax := LEN(aValues)
FOR ii := 1 TO iMax
aValues[ii] := FieldGet(ii)
NEXT
iMax := LEN(aValues): This line gets the length (the number of elements) of the array aValues and assigns it to the variable iMax.
FOR ii := 1 TO iMax: This line starts a FOR loop that will iterate from 1 to iMax, effectively iterating over each index in the array aValues.
aValues[ii] := FieldGet(ii): Inside the loop, this line assigns the value of the iith field from the current work area to the iith element of the array aValues.
NEXT: This line signifies the end of the FOR loop. After this line is executed, if there are still iterations left in the loop (i.e., if ii is less than iMax), the program jumps back to the FOR statement and increments ii by 1. If there are no iterations left (i.e., if ii is equal to iMax), the program continues with the line following the NEXT statement.
In summary, both pieces of code are doing the same thing: assigning each element in the array aValues to the value of the corresponding field from the current work area. The difference lies in the way they're written: the first example uses a built-in function and an anonymous function, while the second example uses a FOR loop and explicit variable assignments.
User
And what is wrong or the differences to with this:' AEval( aValues, {|x,i| x := FieldGet(i) } )'
ChatGPT
In Harbour, the AEval() function iterates over each element of an array, applying a specified code block to each element. In the code block, x represents the current element of the array, and i represents its index.
In your original code, AEval( aValues, {|x,i| aValues[ i ] := FieldGet(i) } ), you're directly modifying the aValues array by assigning the value from FieldGet(i) to the ith position in aValues.
In this new code, AEval( aValues, {|x,i| x := FieldGet(i) } ), you're trying to assign the value from FieldGet(i) to x, which represents the current element of aValues. However, this will not have the desired effect of modifying the aValues array.
The key difference is that x is not a reference to the array element, but a copy of the value. As such, changing x does not change the original array. This is why your original code directly modifies aValues[i] instead of x.
In summary, the problem with the new code is that it doesn't actually modify aValues. After running it, aValues will still have its original values.
Antonio Linares wrote:Please try it this way:
AEval( aValues, {|x,i| aValues[ i ] := FieldGet(i) } )
Otto wrote:I conducted a small test and consulted ChatGPT regarding your question.
******************************************************************************
* Äquivalent zu :setData()
******************************************************************************
FUNCTION Scatter( aValues )
IF Valtype( aValues ) <> "A"
aValues := Array( FCount() )
ENDIF
IF ! Empty( aValues )
IF AScan( aValues, {|x| Valtype(x) <> "O" } ) > 0
AEval( aValues, {|x,i| x:=FieldGet(i) },,, .T. )
ELSE
AEval( aValues, {|o| o:setData() } )
ENDIF
ENDIF
RETURN aValues
******************************************************************************
* Äquivalent zu :getData()
******************************************************************************
FUNCTION Gather( aValues )
IF Valtype( aValues ) == "A"
IF AScan( aValues, {|x| Valtype(x) <> "O" } ) > 0
AEval( aValues, {|x,i| FieldPut(i,x) } )
ELSE
AEval( aValues, {|o| o:getData() } )
ENDIF
ENDIF
RETURN aValues
FieldPut(i,x)
x:=FieldGet(i)
AEval( <aArray>, <bBlock>, [<nStart>], [<nCount>], [<lAssign>] ) --> aArray
<lAssign>
Durch den logischen Ausdruck <lAssign> wird bestimmt, ob innerhalb des Codeblocks eine Zuweisung an das übergebene Arrayelement erfolgen darf. Ist <lAssign> gleich .T. (wahr), so wird das Arrayelement dem Codeblock gewissermaßen per Referenz übergeben und eine Zuweisung an den ersten Codeblockparameter wird auch im entsprechenden Arrayelement durchgeführt.
<lAssign>
The logical expression <lAssign> determines whether an assignment to the passed array element may take place within the code block. If <lAssign> is equal to .T. (true), the array element is effectively passed to the code block by reference and an assignment to the first code block parameter is also performed in the corresponding array element.
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 88 guests