AEVAL() : Get Value of FIELD using FieldGet(i)

AEVAL() : Get Value of FIELD using FieldGet(i)

Postby Jimmy » Tue May 23, 2023 3:41 am

hi,

why thy Syntax does not work
Code: Select all  Expand view  RUN
  * AEval( aValues, {|x,i| x := FieldGet(i) } )


i have to use FOR NEXT loop
Code: Select all  Expand view  RUN
   iMax := LEN(aValues)
   FOR ii := 1 TO iMax
      aValues[ii] := FieldGet(ii)
   NEXT
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: AEVAL() : Get Value of FIELD using FieldGet(i)

Postby Antonio Linares » Tue May 23, 2023 5:40 am

Dear Jimmy,

Please try it this way:

AEval( aValues, {|x,i| aValues[ i ] := FieldGet(i) } )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42117
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: AEVAL() : Get Value of FIELD using FieldGet(i)

Postby Otto » Tue May 23, 2023 6:24 am

Dear Jimmy,

I conducted a small test and consulted ChatGPT regarding your question. I find that ChatGPT acts as a private tutor and is extremely helpful.

Best regards,
Otto

Code: Select all  Expand view  RUN
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.




 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6334
Joined: Fri Oct 07, 2005 7:07 pm

Re: AEVAL() : Get Value of FIELD using FieldGet(i)

Postby Jimmy » Wed May 24, 2023 4:55 am

hi Antonio,
Antonio Linares wrote:Please try it this way:
AEval( aValues, {|x,i| aValues[ i ] := FieldGet(i) } )

ok, that work thx
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: AEVAL() : Get Value of FIELD using FieldGet(i)

Postby Jimmy » Wed May 24, 2023 5:22 am

hi Otto,
Otto wrote:I conducted a small test and consulted ChatGPT regarding your question.

you know Xbase++ which have SCATTER() / GATHER() which i use often in my Xbase++ Apps

Code: Select all  Expand view  RUN
******************************************************************************
* Ä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


Code: Select all  Expand view  RUN
******************************************************************************
* Ä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

when remove all lines with "o" you got only
Code: Select all  Expand view  RUN
FieldPut(i,x)

and
Code: Select all  Expand view  RUN
x:=FieldGet(i)


but Xbase++ AEval() have 5 Parameter
Code: Select all  Expand view  RUN
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.

so that is different to Fivewin and i got empty Array ... :?

i have ask ChatGPT but forgot 5th Parameter which Xbase++ have but ChatGPT don´t know it
so i was on wrong Way as Cl*pper/harbour AEVAL() have 4 Parameter
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1732
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 41 guests