Function 'CopyArray' does not work (v5.0)

gerbagerba Member Posts: 37
Hi all!

I am struggling with the function 'CopyArray'.

Documentation describes:
"Use this function to copy one or more elements in an array to a new array."
The Example is:
"If array1 is an integer array with dimension 10, and it contains the numbers from 1 to 10, the following command copies the numbers 6,7,8,9,10 to array2, an integer array with dimension 5:

COPYARRAY (array2,array1,6,5);"

In my case array1 has dimension 8 and array2 has dimension 12.
All I want is to copy all elements of array1 to array 2.
I tried this:
COPYARRAY(array2,array1,1);

And got this message:
"Array dimensions should be identical."

Also this did not work:
COPYARRAY(array2,array1,1,8);

What do I overlook?!
Please help me!

Gerald

Comments

  • BeliasBelias Member Posts: 2,998
    when using copyarray, you have to fill all the elements of the destination array so, in this code you have
    b array = 12
    a array = 8
    i=integer
    I am going to copy 8 elements from the 12 array to the 8 array with this instruction
    FOR i := 1 TO 12 DO BEGIN
      b[i] := i
    END;
    COPYARRAY(a,b,5);
    

    or
    FOR i := 1 TO 12 DO BEGIN
      b[i] := i
    END;
    COPYARRAY(a,b,1,8);
    

    and this returns error
    FOR i := 1 TO 12 DO BEGIN
      b[i] := i
    END;
    COPYARRAY(a,b,1,7);
    
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • DenSterDenSter Member Posts: 8,307
    The message "Array dimensions should be identical" tells you all you need to know, apparently both arrays must have the same dimension property. Read the C/SIDE Reference guide for more detailed information about COPYARRAY.
  • matttraxmatttrax Member Posts: 2,309
    DenSter wrote:
    The message "Array dimensions should be identical" tells you all you need to know

    I disagree. Based on the example provided in Help the array dimensions clearly do NOT have to be identical:
    Example
    If array1 is an integer array with dimension 10, and it contains the numbers from 1 to 10, the following command copies the numbers 6,7,8,9,10 to array2, an integer array with dimension 5.

    COPYARRAY (array2,array1,6,5);

    It's a very bad error message that doesn't tell you what the actual problem is.
  • BeliasBelias Member Posts: 2,998
    matttrax wrote:
    DenSter wrote:
    The message "Array dimensions should be identical" tells you all you need to know

    I disagree. Based on the example provided in Help the array dimensions clearly do NOT have to be identical:
    Example
    If array1 is an integer array with dimension 10, and it contains the numbers from 1 to 10, the following command copies the numbers 6,7,8,9,10 to array2, an integer array with dimension 5.

    COPYARRAY (array2,array1,6,5);

    It's a very bad error message that doesn't tell you what the actual problem is.

    I agree with you mattrax...the error is somewhat misleading...i've never used this function a lot, and at the beginning i also tought it was a bug...and as you can see in my previous post, it's not immediate to solve this problem (at least for me :whistle: )
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • DenSterDenSter Member Posts: 8,307
    matttrax wrote:
    DenSter wrote:
    The message "Array dimensions should be identical" tells you all you need to know
    I disagree. Based on the example provided in Help the array dimensions clearly do NOT have to be identical:
    Example
    If array1 is an integer array with dimension 10, and it contains the numbers from 1 to 10, the following command copies the numbers 6,7,8,9,10 to array2, an integer array with dimension 5.

    COPYARRAY (array2,array1,6,5);

    It's a very bad error message that doesn't tell you what the actual problem is.
    Well I have to disagree with your disagreement Matt :mrgreen:
    He's trying to put 8 elements into an array of 12, which fails because they do not have the same number of elements, the message is crystal clear to me. The example you are quoting there is copying an array of 5 (5 elements starting with element number 6) into an array of 5, which has the same number of elements. Granted the source array has 10 elements, but the command only tries to copy 5 elements into an array of 5 elements. It's a whole nother discussion as to why they wouldn't allow a small array to be copied into a larger array, but the message itself is very clear to me.

    It's clear that the COPYARRAY command doesn't work, so program a little loop and you're done.
  • BeliasBelias Member Posts: 2,998
    but the COPYARRAY DOES work!
    maybe not exactly in the way you expect, but it works!
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • DenSterDenSter Member Posts: 8,307
    It doesn't work in a situation where you want to copy an array of 8 elements into an array of 12, and he's going to have to find an alternative.

    Here's the syntax for COPYARRAY:
    COPYARRAY(NewArray, Array, Position [, Length])

    The last parameters is Length. If you have a 10 element array, and you want to copy 5 of its elements into a 5 element array, you specify length as 5, just like the example that specified Position=6 and Length=5 to copy into a 5 element array. At that moment, your target as well as your source are both 5 elements, and COPYARRAY will work. It doesn't look at the actual source array, but at the 5 elements you're providing.

    Apparently the command doesn't like it when your source array has fewer elements than the target array, it just needs identical numbers of elements. It wants you to provide as many elements as the target array.

    It works exactly the way that I expect it to work, and the message is crystal clear to me. It means that if you want to copy x elements into an array that has more than x elements, you're going to have to loop through the elements in C/AL code and assign them that way.
  • DenSterDenSter Member Posts: 8,307
    So what I said before:
    DenSter wrote:
    The message "Array dimensions should be identical" tells you all you need to know, apparently both arrays must have the same dimension property.
    Is not entirely correct. It doesn't look at the Dimension property of the variable, but the number of elements that are provided by COPYARRAY. So, Matt, I stand corrected, the message indeed doesn't actually say what is wrong. BUT having worked with NAV for all this time I guess I'm used to interpreting these messages, and have never found this particular message to be unclear.
  • BeliasBelias Member Posts: 2,998
    DenSter wrote:
    It doesn't work in a situation where you want to copy an array of 8 elements into an array of 12, and he's going to have to find an alternative.

    Here's the syntax for COPYARRAY:
    COPYARRAY(NewArray, Array, Position [, Length])

    The last parameters is Length. If you have a 10 element array, and you want to copy 5 of its elements into a 5 element array, you specify length as 5, just like the example that specified Position=6 and Length=5 to copy into a 5 element array. At that moment, your target as well as your source are both 5 elements, and COPYARRAY will work. It doesn't look at the actual source array, but at the 5 elements you're providing.

    Apparently the command doesn't like it when your source array has fewer elements than the target array, it just needs identical numbers of elements. It wants you to provide as many elements as the target array.

    It works exactly the way that I expect it to work, and the message is crystal clear to me. It means that if you want to copy x elements into an array that has more than x elements, you're going to have to loop through the elements in C/AL code and assign them that way.
    yes, the strange thing of the function is the fact that you can't copy 3 elements in a array with max 5 elements...THIS is the thing that confused me.
    about the error: you have two array of 5 elements, then you fill only the first 4 of the first array.
    then you use the copyarray function to move the elements to the 2nd array and BANG!error...
    the first thing I think is to take a look to array dimensions, which are the same...after that...what I have to do if i follow the error as is?
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • BeliasBelias Member Posts: 2,998
    DenSter wrote:
    So what I said before:
    DenSter wrote:
    The message "Array dimensions should be identical" tells you all you need to know, apparently both arrays must have the same dimension property.
    Is not entirely correct. It doesn't look at the Dimension property of the variable, but the number of elements that are provided by COPYARRAY. So, Matt, I stand corrected, the message indeed doesn't actually say what is wrong. BUT having worked with NAV for all this time I guess I'm used to interpreting these messages, and have never found this particular message to be unclear.
    :mrgreen: exactly what I want to say
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • absolutelyfreewebabsolutelyfreeweb Member Posts: 104
    Sometimes you get sad reading threads. You haven't got a single helpful reply.

    forget copyarray it not working as documented, use your own, its pretty simple:

    FOR i := 1 TO ARRAYLEN(OriginalArray) DO
    NewArray := OriginalArray;
Sign In or Register to comment.