Returning an array

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?
SystemAdmin - Fri Jun 26 04:53:19 EDT 2009

Re: Returning an array
Tippers - Fri Jun 26 07:47:57 EDT 2009

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!

Re: Returning an array
SystemAdmin - Fri Jun 26 07:53:27 EDT 2009

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)

Re: Returning an array
Tippers - Fri Jun 26 08:11:43 EDT 2009

SystemAdmin - Fri Jun 26 07:53:27 EDT 2009
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.

 

Re: Returning an array
SystemAdmin - Fri Jun 26 08:18:49 EDT 2009

SystemAdmin - Fri Jun 26 07:53:27 EDT 2009
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)

Re: Returning an array
SystemAdmin - Fri Jun 26 08:22:17 EDT 2009

SystemAdmin - Fri Jun 26 07:53:27 EDT 2009
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

Re: Returning an array
SystemAdmin - Fri Jun 26 08:25:28 EDT 2009

SystemAdmin - Fri Jun 26 08:18:49 EDT 2009

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)

ian_green, thanx! your code does not work.

Re: Returning an array
llandale - Fri Jun 26 09:14:23 EDT 2009

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[])

Re: Returning an array
SystemAdmin - Fri Jun 26 10:09:55 EDT 2009

llandale - Fri Jun 26 09:14:23 EDT 2009
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.

Re: Returning an array
llandale - Fri Jun 26 11:32:21 EDT 2009

SystemAdmin - Fri Jun 26 10:09:55 EDT 2009
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"

Re: Returning an array
llandale - Fri Jun 26 11:39:33 EDT 2009

llandale - Fri Jun 26 11:32:21 EDT 2009

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"

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

Re: Returning an array
SystemAdmin - Thu May 27 10:25:29 EDT 2010

llandale - Fri Jun 26 11:39:33 EDT 2009
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.

Re: Returning an array
SystemAdmin - Thu May 27 10:41:10 EDT 2010

SystemAdmin - Thu May 27 10:25:29 EDT 2010
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.

Re: Returning an array
Mathias Mamsch - Fri May 28 15:56:33 EDT 2010

SystemAdmin - Thu May 27 10:41:10 EDT 2010

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.

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

 

Re: Returning an array
SystemAdmin - Tue Jun 01 09:51:13 EDT 2010

Mathias Mamsch - Fri May 28 15:56:33 EDT 2010

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

 

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)?

 

Re: Returning an array
SystemAdmin - Tue Oct 18 04:51:51 EDT 2011

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

 

Re: Returning an array
Mathias Mamsch - Tue Oct 18 06:21:37 EDT 2011

SystemAdmin - Tue Oct 18 04:51:51 EDT 2011

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

 

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

 

Re: Returning an array
SystemAdmin - Tue Oct 18 07:16:36 EDT 2011

Mathias Mamsch - Tue Oct 18 06:21:37 EDT 2011

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

 

Hi Mathias,

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

Regards, Paul

Re: Returning an array
Mathias Mamsch - Wed Oct 19 02:56:10 EDT 2011

SystemAdmin - Tue Oct 18 07:16:36 EDT 2011
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