scatter / gather FoxPro commands

This forum is meant for questions about the Visual FoxPro Language support in X#.

Post Reply
jpmoschi
Posts: 77
Joined: Thu May 21, 2020 3:45 pm

scatter / gather FoxPro commands

Post by jpmoschi »

Good morning forum,
I continue my task of testing xsharp migrating from foxpro I would like to know the status of the very useful commands scatter and gather.At least the following variants that allow copying the current row from one table to another with the same or similar structure
select SOURCEALIAS
scatter memo to MIAYYAR
or
SCATTER memo MEMVAR
select DESTINATIONALIAS
APPEND BLANK
gather from MIARRAY memo
or
gather memo memvar


In case they do not exist and you need to develop them what would be the best way. Are there similar functions in other dialects?
Thanks
Juan
User avatar
robert
Posts: 4558
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

scatter / gather FoxPro commands

Post by robert »

Juan,

SCATTER, GATHER, COPY TO ARRAY and APPEND FROM ARRAY are all on our todo list.

If you want to do something like a scatter/ gather to an from an array for example, your code could look like this (limited error checking in this example, assuming you want all fields, including memos)

Code: Select all

FUNCTION ScatterToArray() AS ARRAY
LOCAL aResult as ARRAY
LOCAL nFld        AS DWORD
LOCAL nMax       as DWORD
nMax := FCOUNT()
aResult := ArrayNew(nMax)
FOR nFld := 1 to nMax
    aResult[nFld] := FieldGet(nFld)
NEXT
RETURN aResult

FUNCTION GatherFromArray(aData as ARRAY) AS LOGIC
LOCAL nFld   AS DWORD
LOCAL nMax  AS DWORD
nMax := Min(ALen(aData), FCOUNT())
FOR nFld := 1 to nMax
    TRY
         FieldPut(nFld, aData[nFld])
    CATCH
          RETURN FALSE   // FieldType error ?
     END TRY
NEXT
RETURN ALen(aData) == nMax   // return FALSE when the array is shorter or longer than FCount()
A Scatter to/from memvars could use the function FieldName() to retrieve the name of each field and then MemVarGet() or MemVarPut() to read/write the memory variables.
To use a like or an Except clause you would have to match the field names with the pattern with the Like() function.

Scatter to Object would create an instance of the Empty class and then call the AddProperty() method on this class to create properties for each field in the cursor.

I think that the SCATTER command could/would call a couple of different functions, depending on the options:
- ScatterToArray() // all fields
- ScatterToArray(fieldNames, fieldNameMask, LikeOrExcept, lMemo, lBlank) // list of fields, mask, memo and blank clause
- ScatterToMemVars()
- ScatterToMemVars(fieldNames, fieldNameMask, LikeOrExcept, lMemo, lBlank)
- ScatterToObject()
- ScatterToObject(fieldNames, fieldNameMask, LikeOrExcept, lMemo, lBlank)

And similar functions would have to be created for Gather
COPY TO ARRAY and APPEND FROM ARRAY would repeatedly call the Array functions for one or more records.


Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
jpmoschi
Posts: 77
Joined: Thu May 21, 2020 3:45 pm

scatter / gather FoxPro commands

Post by jpmoschi »

To solve it and also learn how to integrate with .Net types:
How can I do the same functions of your example but saving column name and value to make gatherFrom work fine according to the target structure
* returns a dictionary (key, value) with the name and value of each field of the current record in the current workarea
FUNCTION ScatterToDictionary () AS Dictionary <string, object>
* replaces the value of each field that exists in the current row and workarea
FUNCTION GatherFromDictionary (Dictionary <string, object>)
User avatar
robert
Posts: 4558
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

scatter / gather FoxPro commands

Post by robert »

Juan,

Yes that would work. I would recommend Dictionary <string, usual>, since the field values are usuals anyway, so you need less conversions.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
jpmoschi
Posts: 77
Joined: Thu May 21, 2020 3:45 pm

scatter / gather FoxPro commands

Post by jpmoschi »

Thanks Robert, my devolution: Tipical VFP scatter /gather MEVAR commands
FUNCTION ScatterTo() AS Dictionary<STRING,USUAL>
oReturn := Dictionary<STRING,USUAL>{}
LOCAL nFld AS DWORD
LOCAL nMax as DWORD
nMax := FCOUNT()
FOR nFld := 1 to nMax
cKey := FieldName(nFld)
cValue:= FieldGet(nFld)
oReturn:Add( cKey, cValue )
NEXT
RETURN oReturn
*****************************************************
FUNCTION GatherFrom(aData AS Dictionary<STRING,USUAL>) AS LOGIC
LOCAL nFld AS DWORD
LOCAL nMax AS DWORD
local actual:= alias()
foreach f as KeyValuePair<STRING,USUAL> in aData
TRY
fieldputalias(actual,f:Key,f:Value)
CATCH e as Exception
d_log ("gather error" + f:key + ":"+ transform(f:Value,""), e:ToString())
RETURN FALSE
END TRY
endfor
return true
User avatar
robert
Posts: 4558
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

scatter / gather FoxPro commands

Post by robert »

Juan,

Yes that would work.
I would personally in the Gather also use a loop to loop through the field names and then use a aData:ContainsKey() to check of the dictionary contains the field name.
That has the advantage that the 2 workareas do not have to have the same structure. Extra fields in the source table will not lead to exceptions then.
And you can also log then if there are fields in the target table that are not included in the dictionary.
Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
FoxProMatt

scatter / gather FoxPro commands

Post by FoxProMatt »

These functions can be used for new dev work, but, can they soon be wrapped with proper SCATTER / GATHER *commands* so that existing VFP code will run?
User avatar
lumberjack
Posts: 729
Joined: Fri Sep 25, 2015 3:11 pm
Location: South Africa

scatter / gather FoxPro commands

Post by lumberjack »

Hi Matt,
FoxProMatt wrote:These functions can be used for new dev work, but, can they soon be wrapped with proper SCATTER / GATHER *commands* so that existing VFP code will run?
I tried to explain the basics in this thread of last year. Should give somebody in the VFP community a hint how to do something like this.
The thread:Click here
______________________
Johan Nel
Boshof, South Africa
FoxProMatt

scatter / gather FoxPro commands [Issue #387]

Post by FoxProMatt »

BTW - This item is listed as Issue #387 on GitHub:

https://github.com/X-Sharp/XSharpPublic/issues/387

So, It might be more helpful to post any further discussion about this topic in that Issue thread.
Post Reply