Welcome, Guest
Username: Password: Remember me
This public forum is meant for questions and discussions about Visual FoxPro
  • Page:
  • 1

TOPIC:

scatter / gather FoxPro commands 02 Jul 2020 01:20 #15087

  • jpmoschi
  • jpmoschi's Avatar
  • Topic Author


  • Posts: 76
  • 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

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 02 Jul 2020 11:21 #15088

    • robert
    • robert's Avatar


  • Posts: 3595
  • 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)
    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

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 02 Jul 2020 22:05 #15095

    • jpmoschi
    • jpmoschi's Avatar
    • Topic Author


  • Posts: 76
  • 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>)

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 03 Jul 2020 05:30 #15101

    • robert
    • robert's Avatar


  • Posts: 3595
  • 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

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 06 Jul 2020 21:57 #15122

    • jpmoschi
    • jpmoschi's Avatar
    • Topic Author


  • Posts: 76
  • 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

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 07 Jul 2020 08:57 #15124

    • robert
    • robert's Avatar


  • Posts: 3595
  • 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

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 08 Jul 2020 02:07 #15138

    • FoxProMatt
    • FoxProMatt's Avatar



    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?

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands 08 Jul 2020 07:48 #15139

    • lumberjack
    • lumberjack's Avatar


  • Posts: 721
  • 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

    Please Log in or Create an account to join the conversation.

    scatter / gather FoxPro commands [Issue #387] 15 Jul 2020 23:03 #15228

    • FoxProMatt
    • FoxProMatt's Avatar



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

    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.

    Please Log in or Create an account to join the conversation.

    Last edit: by FoxProMatt.
    • Page:
    • 1