Topic
  • 18 replies
  • Latest Post - ‏2011-10-19T06:56:10Z by Mathias Mamsch
SystemAdmin
SystemAdmin
3180 Posts

Pinned topic Returning an array

‏2009-06-26T08:53:19Z |
Hi,

I need a function which returns a string array.

I tried this was, but did not work

string] func(string s[) {
return s
}

Further, I am not able to use the function interface "void func(string& s[])"

Is there a way to return an array of strings....

Can I use pointers in DXL?
Updated on 2011-10-19T06:56:10Z at 2011-10-19T06:56:10Z by Mathias Mamsch
  • Tippers
    Tippers
    16 Posts

    Re: Returning an array

    ‏2009-06-26T11:47:57Z  
    Before we answer that question, can you add the {code} codeword around your code, so that we can see the code you've tried?

    Thanks!
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2009-06-26T11:53:27Z  
    string] sortAlphabetically(string s[) {
    int i, j
    print (sizeof s)
    for (i = 0; i < sizeof s; i++) {
    for (j = 1; j < sizeof s; j++) {
    string temp
    if ( sj-1 > s[j] ) {
    temp = sj-1
    sj-1 = s[j]
    s[j] = temp
    }
    }
    }

    for (i = 0; i < sizeof s; i++) {

    print s[i] "\n"
    }

    return s
    }

    string t[] = {"b", "anil", "aa", "zuzuz", "suss"}

    t = sortAlphabetically(t)
  • Tippers
    Tippers
    16 Posts

    Re: Returning an array

    ‏2009-06-26T12:11:43Z  
    string] sortAlphabetically(string s[) {
    int i, j
    print (sizeof s)
    for (i = 0; i < sizeof s; i++) {
    for (j = 1; j < sizeof s; j++) {
    string temp
    if ( sj-1 > s[j] ) {
    temp = sj-1
    sj-1 = s[j]
    s[j] = temp
    }
    }
    }

    for (i = 0; i < sizeof s; i++) {

    print s[i] "\n"
    }

    return s
    }

    string t[] = {"b", "anil", "aa", "zuzuz", "suss"}

    t = sortAlphabetically(t)

    Arrays are passed by reference, not value. So if you change the passed array within the function, the original array will be changed.

    Try this:

    string ss[] = {"first", "second", "third"}
     
    void func (string s[]) {
        string temp
            print "start of function: " s[1] "\n"
            temp = s[0]
            s[0] = s[1]
            s[1] = s[2]
            s[2] = temp
            print "end of function: " s[1] "\n"
    }
     
    print "before function: " ss[1] "\n"
     
    func (ss)
     
    print "after function: " ss[1] "\n"
    

     


    I get the following as output:

    before function: second
    start of function: second
    end of function: third
    after function: third

    Paul.

     

    Updated on 2013-12-18T19:00:55Z at 2013-12-18T19:00:55Z by JAntley
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2009-06-26T12:18:49Z  
    string] sortAlphabetically(string s[) {
    int i, j
    print (sizeof s)
    for (i = 0; i < sizeof s; i++) {
    for (j = 1; j < sizeof s; j++) {
    string temp
    if ( sj-1 > s[j] ) {
    temp = sj-1
    sj-1 = s[j]
    s[j] = temp
    }
    }
    }

    for (i = 0; i < sizeof s; i++) {

    print s[i] "\n"
    }

    return s
    }

    string t[] = {"b", "anil", "aa", "zuzuz", "suss"}

    t = sortAlphabetically(t)

    Hello,
    you have incorrectly declared the type of "sortAlphabetically". Here is the correct definition:
     

    string sortAlphabetically(string s[])[]
    {
        int i, j;
        print (sizeof s);
        for (i = 0; i < sizeof s; i++) {
            for (j = 1; j < sizeof s; j++)
            {
                string temp
                    if ( s[j-1] > s[j] )
                    {
                        temp = s[j-1];
                        s[j-1] = s[j];
                        s[j] = temp;
                    }
            }
        }
        for (i = 0; i < sizeof s; i++) {
            print s[i] "\n";
        }
        return s;
    }
     
    string t[] = {"b", "anil", "aa", "zuzuz", "suss"}
     
    t = sortAlphabetically(t)
    
    Updated on 2013-12-18T19:01:38Z at 2013-12-18T19:01:38Z by JAntley
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2009-06-26T12:22:17Z  
    string] sortAlphabetically(string s[) {
    int i, j
    print (sizeof s)
    for (i = 0; i < sizeof s; i++) {
    for (j = 1; j < sizeof s; j++) {
    string temp
    if ( sj-1 > s[j] ) {
    temp = sj-1
    sj-1 = s[j]
    s[j] = temp
    }
    }
    }

    for (i = 0; i < sizeof s; i++) {

    print s[i] "\n"
    }

    return s
    }

    string t[] = {"b", "anil", "aa", "zuzuz", "suss"}

    t = sortAlphabetically(t)
    Hi Paul,

    you did not get my question. I have no problems in passing an array to function. This works....

    But, I want to RETURN an array with a function. Here I have problems.

    For eg. I tried,
    string] func(string s[) {return s}
    ian_green, your code does not work
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2009-06-26T12:25:28Z  

    Hello,
    you have incorrectly declared the type of "sortAlphabetically". Here is the correct definition:
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">string sortAlphabetically(string s[])[] { int i, j; print (sizeof s); for (i = 0; i < sizeof s; i++) { for (j = 1; j < sizeof s; j++) { string temp if ( s[j-1] > s[j] ) { temp = s[j-1]; s[j-1] = s[j]; s[j] = temp; } } } for (i = 0; i < sizeof s; i++) { print s[i] "\n"; } return s; } string t[] = {"b", "anil", "aa", "zuzuz", "suss"} t = sortAlphabetically(t) </pre>
    ian_green, thanx! your code does not work.
  • llandale
    llandale
    3035 Posts

    Re: Returning an array

    ‏2009-06-26T13:14:23Z  
    Don't use the ampersand in the function parameter definition. You can change the array regardless, using the ampersand messes it up.

    use:.......... void MyFunct(string s[])
    instead of:... void MyFunct(string &s[])
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2009-06-26T14:09:55Z  
    • llandale
    • ‏2009-06-26T13:14:23Z
    Don't use the ampersand in the function parameter definition. You can change the array regardless, using the ampersand messes it up.

    use:.......... void MyFunct(string s[])
    instead of:... void MyFunct(string &s[])
    Hi, still my problem is addressed. I want to RETURN an array using a function.
  • llandale
    llandale
    3035 Posts

    Re: Returning an array

    ‏2009-06-26T15:32:21Z  
    Hi, still my problem is addressed. I want to RETURN an array using a function.
    string    GetArray(int Size)[]
    {
            string  Arr[Size]
            int     i
            for (i=0; i<Size; i++)
               Arr[i] = (i*2) ""
            return(Arr)
    }
     
    string Arr[]
    Arr = GetArray(5)
    int   i
    for (i=0; i<5; i++)
       print Arr[i] "\n"
    
    Updated on 2013-12-18T19:02:17Z at 2013-12-18T19:02:17Z by JAntley
  • llandale
    llandale
    3035 Posts

    Re: Returning an array

    ‏2009-06-26T15:39:33Z  
    • llandale
    • ‏2009-06-26T15:32:21Z
    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">string GetArray(int Size)[] { string Arr[Size] int i for (i=0; i<Size; i++) Arr[i] = (i*2) "" return(Arr) } string Arr[] Arr = GetArray(5) int i for (i=0; i<5; i++) print Arr[i] "\n" </pre>
    Golly, it just occurred to me that this method can solve some of the populate-list-DBE problems we've been having. We can declare the array generically in the main program, and calculate how big it is later and populate it at that size with a function like this.

    • Louie
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2010-05-27T14:25:29Z  
    • llandale
    • ‏2009-06-26T15:39:33Z
    Golly, it just occurred to me that this method can solve some of the populate-list-DBE problems we've been having. We can declare the array generically in the main program, and calculate how big it is later and populate it at that size with a function like this.

    • Louie
    I tried this and it may work once or twice but will eventually fail miserably.

    There is nothing wrong with the syntax. The root of the problem is the function is returning a local stack item that has gone out of scope. Since it has gone out of scope, DOORS is free to re-use the memory. If it appears to work, it is only because DOORS has not reused the stack memory yet.

    DOORS GUI items like checkBox require an parameter of type "string storage must be valid when the dialog box is shown (not just when checkBox is added). The most straightforward way to ensure the scope is valid is to use string[] variables declared globally. This is generally counter to good programming practices.

    As an alternative to the global variable technique, I've come up with an alternative. The basic technique is:

    1) Declare the string[] as you normally would in a function (in stack space).
    2) Create a Buffer (whose storage exists until you delete it).
    3) Give the Buffer object enough internal storage to hold the whole string[] data structure including referenced strings by using the Buffer's length(Buffer,size) function.
    4) Clone the entire string[] data structure and also copy of the referenced strings into the Buffer and point to the internal copies
    5) return the appropriate address within the Buffer as the string[] function return parameter.
    6) After dialog lifespan is over, delete the Buffer.

    I've been using this for a while and it has been working like a champ so far.

    The root of the problem is some DXL functions require standard arrays (e.g., string[]) as parameters but there is no dynamic create/delete).

    usage:

    string getNonStackCopy(string localArrayOfStrings[],
    Buffer resourceToDeleteLater)[] {
    // ...
    return (appropriate address within buffer cast as string[])
    }

    // within a function ...
    string colors[] = { "red", "blue", "green" };
    Buffer resourceToDeleteLater = create(); // must keep track of this to delete later
    string nonStackColors = getNonStackCopy(colors, resourceToDeleteLater );
    // Now nonStackColors will reliably exist until resourceToDeleteLater is deleted.

    If anyone is interested I could make a single-module version to see if anyone can break it.
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2010-05-27T14:41:10Z  
    I tried this and it may work once or twice but will eventually fail miserably.

    There is nothing wrong with the syntax. The root of the problem is the function is returning a local stack item that has gone out of scope. Since it has gone out of scope, DOORS is free to re-use the memory. If it appears to work, it is only because DOORS has not reused the stack memory yet.

    DOORS GUI items like checkBox require an parameter of type "string storage must be valid when the dialog box is shown (not just when checkBox is added). The most straightforward way to ensure the scope is valid is to use string[] variables declared globally. This is generally counter to good programming practices.

    As an alternative to the global variable technique, I've come up with an alternative. The basic technique is:

    1) Declare the string[] as you normally would in a function (in stack space).
    2) Create a Buffer (whose storage exists until you delete it).
    3) Give the Buffer object enough internal storage to hold the whole string[] data structure including referenced strings by using the Buffer's length(Buffer,size) function.
    4) Clone the entire string[] data structure and also copy of the referenced strings into the Buffer and point to the internal copies
    5) return the appropriate address within the Buffer as the string[] function return parameter.
    6) After dialog lifespan is over, delete the Buffer.

    I've been using this for a while and it has been working like a champ so far.

    The root of the problem is some DXL functions require standard arrays (e.g., string[]) as parameters but there is no dynamic create/delete).

    usage:

    string getNonStackCopy(string localArrayOfStrings[],
    Buffer resourceToDeleteLater)[] {
    // ...
    return (appropriate address within buffer cast as string[])
    }

    // within a function ...
    string colors[] = { "red", "blue", "green" };
    Buffer resourceToDeleteLater = create(); // must keep track of this to delete later
    string nonStackColors = getNonStackCopy(colors, resourceToDeleteLater );
    // Now nonStackColors will reliably exist until resourceToDeleteLater is deleted.

    If anyone is interested I could make a single-module version to see if anyone can break it.

    I found a few typos. See this instead:
    I tried this and it may work once or twice but will eventually fail miserably.

    There is nothing wrong with the syntax. The root of the problem is the function is returning a local stack item that has gone out of scope. Since it has gone out of scope, DOORS is free to re-use the memory. If it appears to work, it is only because DOORS has not reused the stack memory yet.

    DOORS GUI items like checkBox require an parameter of type "string variables declared globally. This is generally counter to good programming practices.

    As an alternative to the global variable technique, I've come up with an alternative. The basic technique is:

    1) Declare the string[] as you normally would in a function (in stack space).
    2) Create a Buffer (whose storage exists until you delete it).
    3) Give the Buffer object enough internal storage to hold the whole string[] data structure including referenced strings by using the Buffer's length(Buffer,size) function.
    4) Clone the entire string[] data structure and also copy of the referenced strings into the Buffer and point to the internal copies
    5) return the appropriate address within the Buffer as the string[] function return parameter.
    6) After dialog lifespan is over, delete the Buffer.

    I've been using this for a while and it has been working like a champ so far.

    The root of the problem is some DXL functions require standard arrays (e.g., string[]) as parameters but there is no dynamic create/delete).

    usage:

    string getNonStackCopy(string localArrayOfStrings[],
                           Buffer resourceToDeleteLater)[] {
        // ...
        return (appropriate address within buffer cast as string[])
    }
     
    // within a function ...
    string colors[] = { "red", "blue", "green" };
    Buffer resourceToDeleteLater = create(); // must keep track of this to delete later
    string nonStackColors[];
    nonStackColors = getNonStackCopy(colors, resourceToDeleteLater );
    // Now nonStackColors will reliably exist until resourceToDeleteLater is deleted.
    


    If anyone is interested I could make a single-module version to see if anyone can break it.

    Updated on 2013-12-18T19:02:59Z at 2013-12-18T19:02:59Z by JAntley
  • Mathias Mamsch
    Mathias Mamsch
    2147 Posts

    Re: Returning an array

    ‏2010-05-28T19:56:33Z  

    I found a few typos. See this instead:
    I tried this and it may work once or twice but will eventually fail miserably.

    There is nothing wrong with the syntax. The root of the problem is the function is returning a local stack item that has gone out of scope. Since it has gone out of scope, DOORS is free to re-use the memory. If it appears to work, it is only because DOORS has not reused the stack memory yet.

    DOORS GUI items like checkBox require an parameter of type "string variables declared globally. This is generally counter to good programming practices.

    As an alternative to the global variable technique, I've come up with an alternative. The basic technique is:

    1) Declare the string[] as you normally would in a function (in stack space).
    2) Create a Buffer (whose storage exists until you delete it).
    3) Give the Buffer object enough internal storage to hold the whole string[] data structure including referenced strings by using the Buffer's length(Buffer,size) function.
    4) Clone the entire string[] data structure and also copy of the referenced strings into the Buffer and point to the internal copies
    5) return the appropriate address within the Buffer as the string[] function return parameter.
    6) After dialog lifespan is over, delete the Buffer.

    I've been using this for a while and it has been working like a champ so far.

    The root of the problem is some DXL functions require standard arrays (e.g., string[]) as parameters but there is no dynamic create/delete).

    usage:

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">string getNonStackCopy(string localArrayOfStrings[], Buffer resourceToDeleteLater)[] { // ... return (appropriate address within buffer cast as string[]) } // within a function ... string colors[] = { "red", "blue", "green" }; Buffer resourceToDeleteLater = create(); // must keep track of this to delete later string nonStackColors[]; nonStackColors = getNonStackCopy(colors, resourceToDeleteLater ); // Now nonStackColors will reliably exist until resourceToDeleteLater is deleted. </pre>


    If anyone is interested I could make a single-module version to see if anyone can break it.

    It is a clever solution, but there are some things that I don't like:

    1. You need to declare a Buffer and delete it later to free the memory. Its not transparent for the developer that after deleting the Buffer the array will be gone. Instead make your own datatype! Call it PersistentArray or something. Define a function _d getArray (PersistentArray) [] to return an array of any kind. Store the handles to the arrays and set them to uninitialized on the void delete (PersistentArray).

    2. Its only for strings. I think you should be able to store any kind of data in there.

    3. I don't like the idea of replicating the internal memory structure of an array inside a Buffer memory (yuck!). You don't know the memory management of DOORS (if there is any) will mess with your data once a certain amount of memory is filled or whatever.

    There are about 25 functions in the DXL perms that take arrays as parameters. So why would anyone want to write code that returns arrays? Because you want to pass the array to your listView ? Why not do this?
     

    Array getColors (...){ ... // returns colors array ... }
    // ...
     
    Array cols = getColors (...); 
    string locAr[size cols]; locAr <- cols; 
     
    DBE list = listView (myDialog, ..., locAr) 
    // ...
    

     


    You would only need to define the int size(Array) and ::<- (_d[], Array) perm. Then you can eliminate those one dimensional arrays from your code and whenever you need to call a perm that needs an array you can use this kind of code.

    Just my opinion, regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2013-12-18T19:04:04Z at 2013-12-18T19:04:04Z by JAntley
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2010-06-01T13:51:13Z  

    It is a clever solution, but there are some things that I don't like:

    1. You need to declare a Buffer and delete it later to free the memory. Its not transparent for the developer that after deleting the Buffer the array will be gone. Instead make your own datatype! Call it PersistentArray or something. Define a function _d getArray (PersistentArray) [] to return an array of any kind. Store the handles to the arrays and set them to uninitialized on the void delete (PersistentArray).

    2. Its only for strings. I think you should be able to store any kind of data in there.

    3. I don't like the idea of replicating the internal memory structure of an array inside a Buffer memory (yuck!). You don't know the memory management of DOORS (if there is any) will mess with your data once a certain amount of memory is filled or whatever.

    There are about 25 functions in the DXL perms that take arrays as parameters. So why would anyone want to write code that returns arrays? Because you want to pass the array to your listView ? Why not do this?
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">Array getColors (...){ ... // returns colors array ... } // ... Array cols = getColors (...); string locAr[size cols]; locAr <- cols; DBE list = listView (myDialog, ..., locAr) // ... </pre>

     


    You would only need to define the int size(Array) and ::<- (_d[], Array) perm. Then you can eliminate those one dimensional arrays from your code and whenever you need to call a perm that needs an array you can use this kind of code.

    Just my opinion, regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Thanks for your reply.

    There is actually one (and only one) need driving this solution -- namely, checkBoxes. One has to pass a regular array of strings (string[]) that has precise size. There is no extra parameter to say "only consider the first 11 members." The memory that holds the checkBox labels must be valid an reliable not just when the items added to the dialog, but throughout the dialog's lifecycle.

    checkBox 
    Declaration 
    DBE {verticalC|c}heckBox(DB box,
                             string label,
                             string choices[],
                             int initial) 
    Operation 
    Creates a set of check boxes. 
     
    Check boxes offers users choices, each of which can independently be either on or off. 
     
    The checkBox function arranges the check boxes horizontally; the verticalCheckBox function arranges them vertically. The options are passed in string array choices. The initial and returned values are bit maps indicating whether each option is checked. If the first option is checked, bit 0 is 1, if the second is checked bit 1 is 1, and so on.
    

     


    Why am I using Buffer? -- because I can't get my hands on a general malloc() function. My assumptions are if I do one size adjustment on Buffer, the memory will be contiguous. I will never resize thereafter. Buffer is the nearest thing I can find to a malloc() simulation.

    There is another problem/solution set that this solves -- namely, checkBox layout. Most often, the displayed checkBoxes correspond to enumerated types. These tend to change in both name and count fairly often. DOORS gives the horizontal layout and the vertical layout without any real option for making the lists look nice. The function also seems to limit the checkBox count at 32 (no of bits in an int).

    Here is an option that both looks neat and does not require reprogramming upon enumeration type changes:
    1) Enclose the display of the enumerated checkBoxes within a frame. The frame label holds the enumerated type name.
    2) Inside the frame, I put an arbitrary amount of independent checkBoxes that have no group label and each checkBox group is size one (1).
    3) Using alignment trickery I can make the the total checkBox grid appear with uniform spacing (like a grid) aligning with each other both horizontally (easy) and vertically.
    4) After the dialog form is dismissed, I get a nice list list of checked items in the frame as a Skip (String) list.

    From a high-level, I am:
    1) specifying an module attribute that has an enumerated type.
    2) a name for the frame (usually the enumerated variable name itself)
    3) I get back a Skip list if checked items (which have unique labels anyway).

    The inner details are hidden.

    I'm sure I could add other types of non-object (array[]) types to the functionality, but I have no use-case for it. If it were not for checkBoxes, I would not use the above either.

    Do you (or anyone) know a clean, non-global variable, low-code-maintenance, way to deal with lots checkBoxes (say from ever-changing enumerated types)?

     

    Updated on 2013-12-18T19:04:31Z at 2013-12-18T19:04:31Z by JAntley
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2011-10-18T08:51:51Z  

    Hi Mathias/or anybody else who knows,

    I want to implement something that was suggested earlier in this post

    Array getColors (...){ ... // returns colors array ... }
    // ...
     
    Array cols = getColors (...); 
    string locAr[size cols]; locAr <- cols; 
     
    DBE list = listView (myDialog, ..., locAr)
    

     


    I'm just totally lost in trying to fathom out how to implement the and ::<- (_d[], Array) perm that you suggested.
    Have been unsuccessfully messing around with the addresses and string (*ArrayPtr)[]
    i.e. casting Array to string[] ?

    Can you please give some tips ?

    Thanks, Paul

     

    Updated on 2013-12-18T19:12:29Z at 2013-12-18T19:12:29Z by JAntley
  • Mathias Mamsch
    Mathias Mamsch
    2147 Posts

    Re: Returning an array

    ‏2011-10-18T10:21:37Z  

    Hi Mathias/or anybody else who knows,

    I want to implement something that was suggested earlier in this post

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">Array getColors (...){ ... // returns colors array ... } // ... Array cols = getColors (...); string locAr[size cols]; locAr <- cols; DBE list = listView (myDialog, ..., locAr) </pre>

     


    I'm just totally lost in trying to fathom out how to implement the and ::<- (_d[], Array) perm that you suggested.
    Have been unsuccessfully messing around with the addresses and string (*ArrayPtr)[]
    i.e. casting Array to string[] ?

    Can you please give some tips ?

    Thanks, Paul

     

    It is not straight forward. I made a quick reference implementation. Hope that helps.
    Note that the ::<- operator is only syntactic sugar. Regards, Mathias
     

    // ********************** Library Code ************************
    // Copy the values from an 1d array to an Array
    Array fromArray (_b var[]) {
      int (*ar)[] = &var // type cast so everything works nicely 
      int arSize = sizeof (*ar)
     
      // copy the values of the array into an Array
      Array arResult = create(arSize+1,1) 
      
      put(arResult, arSize, 0,0) // put the size as index 0
      if (arSize > 1) {
          int i; for i in 1:arSize do {
               int vv = (*ar)[i-1] // Read array value as int (don't mind the real type)
               put(arResult,vv, i, 0)  // put that value in the Array
            }
      }
     
      return arResult
    }
     
    // read the size of the array 
    int size (Array a) {
       int iSize = (get (a, 0,0)) int  // we stored the size in element (0,0)
       return iSize
    }
     
    // restore the Array into the 1d-Array
    void restoreArray(_ var[], Array ar) {
      int (*a)[] = &var // type cast so everything works nicely 
      int iSize = (get(ar, 0,0)) int  // get size from array
      int arSize = sizeof (*a)        // get the size of the 1d array
      if (arSize < iSize) {
          print "Invalid Array Size " iSize " supplied to restoreArray. Only storing: " arSize " items.\n"
          iSize = arSize
      }
      
      if (iSize > 0) {
         // Restore 1d-array from Array
         int i; for i in 0:iSize-1 do (*a)[i] = (get (ar, i+1, 0)) int 
      }
    }
     
    // syntactic sugar ...
    void ::<-(_ x[], Array a) { restoreArray (x, a) }
     
     
    // *************** User Code *********************
    Array getItems () {
      string vals[] = {"Item 1", "Item 2", "Item 3", "Item 4"}
      return fromArray(vals)
    }
     
    Array arItems = getItems() 
    print "Size of Get Items Array:" (size arItems) "\n"
     
    string val[size arItems]; val <- arItems
     
    print "Item 1:" val[0] "\n"
    print "Item 2:" val[1] "\n"
    print "Item 3:" val[2] "\n"
    print "Item 4:" val[3] "\n"
    

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2013-12-18T19:13:18Z at 2013-12-18T19:13:18Z by JAntley
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Returning an array

    ‏2011-10-18T11:16:36Z  

    It is not straight forward. I made a quick reference implementation. Hope that helps.
    Note that the ::<- operator is only syntactic sugar. Regards, Mathias
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">// ********************** Library Code ************************ // Copy the values from an 1d array to an Array Array fromArray (_b var[]) { int (*ar)[] = &var // type cast so everything works nicely int arSize = sizeof (*ar) // copy the values of the array into an Array Array arResult = create(arSize+1,1) put(arResult, arSize, 0,0) // put the size as index 0 if (arSize > 1) { int i; for i in 1:arSize do { int vv = (*ar)[i-1] // Read array value as int (don't mind the real type) put(arResult,vv, i, 0) // put that value in the Array } } return arResult } // read the size of the array int size (Array a) { int iSize = (get (a, 0,0)) int // we stored the size in element (0,0) return iSize } // restore the Array into the 1d-Array void restoreArray(_ var[], Array ar) { int (*a)[] = &var // type cast so everything works nicely int iSize = (get(ar, 0,0)) int // get size from array int arSize = sizeof (*a) // get the size of the 1d array if (arSize < iSize) { print "Invalid Array Size " iSize " supplied to restoreArray. Only storing: " arSize " items.\n" iSize = arSize } if (iSize > 0) { // Restore 1d-array from Array int i; for i in 0:iSize-1 do (*a)[i] = (get (ar, i+1, 0)) int } } // syntactic sugar ... void ::<-(_ x[], Array a) { restoreArray (x, a) } // *************** User Code ********************* Array getItems () { string vals[] = {"Item 1", "Item 2", "Item 3", "Item 4"} return fromArray(vals) } Array arItems = getItems() print "Size of Get Items Array:" (size arItems) "\n" string val[size arItems]; val <- arItems print "Item 1:" val[0] "\n" print "Item 2:" val[1] "\n" print "Item 3:" val[2] "\n" print "Item 4:" val[3] "\n" </pre>

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Hi Mathias,

    Phew, not straight forward is an understatement. Thanks a lot, really helps

    Regards, Paul
  • Mathias Mamsch
    Mathias Mamsch
    2147 Posts

    Re: Returning an array

    ‏2011-10-19T06:56:10Z  
    Hi Mathias,

    Phew, not straight forward is an understatement. Thanks a lot, really helps

    Regards, Paul

    I just spotted a probable error:
     

    ...
    if (arSize > 1) {
    ...
    

     


    should probably be:

     

     

     

    ...
    if (arSize > 0) {
    ...
    



    Regards, Mathias



     

     

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Updated on 2013-12-18T19:14:10Z at 2013-12-18T19:14:10Z by JAntley