Adding an Array to Another Array

Hello,

I'm new to DXL and have a simple question. I just want to add all elements of one array to another array. Something like this:

 

for (int i=0;  i < sizeof(array); i++)

{

 array1[i] += array2[i]

}

  Is their a way to do this in DXL? Thanks!


Mosep - Tue Jul 22 08:44:07 EDT 2014

Re: Adding an Array to Another Array
llandale - Tue Jul 22 16:36:36 EDT 2014

New to DXL? I recomend you use the module tools menu to "browse" for dxl.  Get a hard copy of one, then run it, and try to figure out how the code produced the results you see.

DXL "arrays" are pretty weak.  You MUST determine their actual size when you declare them.  They cannot be used as output of a function since when delcared in the function, the space is removed from the stack when the function ends. 

Take a look at "Skip" lists; pretty straight forward replacement for "array"s.

  • Skip skp = create()
  • int indexSkp = 0
  • ..
  • put(skp, indexSkp++, "SomeString")
  • put(skp, indexSkp++, "SomeOtherString")

Back to your problem. 

  • string array1[10]
  • string array2[8]
  • Initialize array contents
  • int i
  • int size1 = sizeof(array1),
  •      size2 = sizeof(array2)
  • int max
  • if (size1 < size2) then max = size1 else max = size2
  • for (i=0; i<max; i++)
  • {  array1[i] = array2[i]
  • }
  • now what to do with array1 elements 8 and 9? 

Your "+=" reference would work if both arrays were type "int" and you wanted to add array2 to array1.  Declaring "i" inside the loop control statement isn't going to work.

-Louie

Re: Adding an Array to Another Array
Mosep - Wed Jul 23 20:27:17 EDT 2014

llandale - Tue Jul 22 16:36:36 EDT 2014

New to DXL? I recomend you use the module tools menu to "browse" for dxl.  Get a hard copy of one, then run it, and try to figure out how the code produced the results you see.

DXL "arrays" are pretty weak.  You MUST determine their actual size when you declare them.  They cannot be used as output of a function since when delcared in the function, the space is removed from the stack when the function ends. 

Take a look at "Skip" lists; pretty straight forward replacement for "array"s.

  • Skip skp = create()
  • int indexSkp = 0
  • ..
  • put(skp, indexSkp++, "SomeString")
  • put(skp, indexSkp++, "SomeOtherString")

Back to your problem. 

  • string array1[10]
  • string array2[8]
  • Initialize array contents
  • int i
  • int size1 = sizeof(array1),
  •      size2 = sizeof(array2)
  • int max
  • if (size1 < size2) then max = size1 else max = size2
  • for (i=0; i<max; i++)
  • {  array1[i] = array2[i]
  • }
  • now what to do with array1 elements 8 and 9? 

Your "+=" reference would work if both arrays were type "int" and you wanted to add array2 to array1.  Declaring "i" inside the loop control statement isn't going to work.

-Louie

Thanks! That was very helpful.