Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Ben R. on April 10, 2014, 09:16:07 pm



Title: Passing a dynamic array of record to a function.
Post by: Ben R. on April 10, 2014, 09:16:07 pm
We have a number of programs that display tables including both database and formonly columns. With this in mind, sometimes it's necessary to have a program default to sorting by a formonly column. I wrote a single-use function for sorting on the particular column in one application, but wanted to convert it into something a bit more reusable. The problem I'm running into is that I'm not seeing a way to pass an array of record to a function without the function specifically declaring the record type.

Here's what I'm running into:

Quote
function sort_array(sortArray, sortColumn)
  define
    sortArray dynamic array of record,
| A grammatical error has been found at ',' expecting: IDENT LIKE.
| See error number -6609.
    sortColumn string,
    sortFlag integer,
    sortRow integer

  let sortFlag = 1
  let arraySize = sortArray.getLength()

  while sortFlag = 1
    let sortFlag = 0

    for sortRow = 1 to arraySize + 1 step 1
      if sortArray[sortRow].sortColumn < sortArray[sortRow + 1].sortColumn then
        let sortFlag = 1
        let sortArray[arraySize + 1].* = sortArray[sortRow].*
        let sortArray[sortRow].* = sortArray[sortRow + 1].*
        let sortArray[sortRow + 1].* = sortArray[arraySize + 1].*
      end if
    end for
  end while

  call sortArray.deleteElement(arraySize + 1)
end function

Any thoughts on how to handle this, or what a better approach to the problem would be? The individual-use version I have works because it's calling on a global array of record that has been previously defined, but that's not a very portable approach.


Title: Re: Passing a dynamic array of record to a function.
Post by: Reuben B. on April 11, 2014, 12:35:20 am
Ask your support contact to add you to list of requestors for Bz3491 https://4js.com/en/support/issue/?id=3491
As the issue notes, the difficulty is handling the case if you sort whilst in a dialog.

For the sort problem, in the past I had a pre-processor directive

&define arraysort(p_arr,p_sort,p_temp,i1,i2) FOR i1 = 1 TO p_arr.getLength() \
        FOR i2 = (i1+1) TO p_arr.getLength() \
            IF p_arr[i1].p_sort > p_arr[i2].p_sort THEN \
                LET p_temp.* = p_arr[i2].* \
                LET p_arr[i2].* = p_arr[i1].* \
                LET p_arr[i1].* = p_temp.* \
            END IF \
        END FOR \
    END FOR

and then I could code ...

arraysort(array-variable,sort-column, temp-variable, idx1, idx2)

... only hassle I recall was having to have the temp-variable and two variables for indices defined.

From 2.50 on, I would investigate writing a function using the JSONObject and JSONArray methods.  A quick test suggests something similar to ...

FUNCTION json_array_sort(ja, sort_col)
DEFINE ja util.JSONArray
DEFINE jo1, jo2,t util.JsonObject
DEFINE sort_col STRING
DEFINE i,j INTEGER
DEFINE type STRING

    FOR i = 1 TO ja.getLength()
        FOR j = (i+1) TO ja.getLength()
            LET jo1 = ja.get(i)
            LET jo2 = ja.get(j)
            IF jo1.get(sort_col) > jo2.get(sort_col) THEN
                LET t = jo2
                CALL ja.put(i,jo2)
                CALL ja.put(j,jo1)
            END IF
        END FOR
    END FOR
END FUNCTION

... could be called by ...

DEFINE ja util.JSONArray

    LET ja = util.JSONArray.fromFGL(array-variable)
    CALL json_array_sort(ja,sort-column)
    CALL ja.toFGL(array-variable)

... there is probably room for improvement but that is a starting point.

It builds on a technique I've used before where my copy entire array to clipboard function used base.Typeinfo so that the function took as input a DomDocument, not an array, and hence could be called by any array e.g.

CALL copy_entire_array_to_clipboard(base.TypeInfo(array-variable))

Hope that helps,

Reuben