Forum Discussion

FICO's avatar
FICO
Community Manager
8 years ago

Question about dynamic arrays

I am creating a package. In this package, I need to first load a file:

->

package test

declarations:

! Raw variables to read the standard format for rate file

RATE_I_SEGMENTS: set of string

RATE_STARTDATEs: set of string

RATE_ENDDATEs: set of string

RATE_I_ARRAY: dynamic array (RATE_I_SEGMENTS, RATE_STARTDATEs, RATE_ENDDATEs) of real

end-declarations

procedure test_init(_sRateFilename: string) 

! Check file

if FileExists(_sRateFilename) = false then

     assert(false)

     return end-if

! Read data

initializations from "mmsheet.csv:" + _sRateFilename

     RATE_I_ARRAY as "skiph;(#1, #2, #3, #4)"

end-initializations

end-procedure

end-package

->

Say now, I would like to call test_init twice consecutively with a different file, and therefore possibly different sets for RATE_I_SEGMENTS,  RATE_STARTDATEs, or RATE_ENDDATEs. If I add delcell(RATE_I_ARRAY) before reading the file, this will clear RATE_I_ARRAY, and I will only get the new data in RATE_I_ARRAY. This is fine. However, RATE_STARTDATEs might carry data from the previous call. This is not necessarily an issue as RATE_I_ARRAY is a dynamic array. Out of curiosity, my question is: is there a way to start with, say RATE_I_SEGMENTS = {}, for any of subsequent calls to test_init. Setting explicitly RATE_I_SEGMENTS := {} in test_init yields the run-time error  Mosel: E-1003: Trying to modify a finalized or fixed set

Thank you

Alexis

1 Reply

  • FICO's avatar
    FICO
    Community Manager

    Once a set has been used as index set for an array it is no longer possible to delete it or even to remove elements from this set - but if the set has not been finalized you can keep adding elements to it.

    If your code requires the exact index set contents of one of your arrays you probably need to create a separate set, eg

    NewSet:= union (s in RATE_I_SEGMENTS, d in RATE_STARTDATEs, e in RATE_ENDDATEs | exists (RATE_I_ARRAY(s,d,e)) {s}

    For the enumeration of the current contents of the dynamic arrays, make sure to always employ the 'exists' test.

    Another note, unrelated to your question: any declarations in a package of entities or subroutines that are meant to be accessible from other parts of your program (that is, from outside of the package) need to be explicitly marked as public'.