配列をコピーしたい

こんにちは。
UiPath UiPath Studio Community Edition 2024.4.0ユーザです。

Copilotに配列arr1のindex番号6~12を配列arr2にコピーする構文は下記になると教えてもらいました。

Array.Copy(arr1, 5, arr2, 0, 7)

左辺と右辺がないため、これを代入アクティビティの代入文にどのように記述すれば良いのか分かりません。もしかしたらForEachアクティビティで回すのでしょうか?YESであれば有識者の方、回し方をご教示ください。

Hi @gorby

=> Use below syntax in Assign activity to intializa the array

Assign activity -> arr1 = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

Assign activity -> arr2 = New Integer(6) {}

Both arr1 and arr2 are of DataType Array(System.Int32)

=> Use the below code in Invoke Code activity

Array.Copy(arr1, 6, arr2, 0, 7)

Invoke Code Arguments:

=> Message Box: String.Join(", ", arr2)

Regards

Hi Thank you for your reply!
What you showed me was what I wanted to see.

1 Like

You’re welcome @gorby

Happy Automation!!

Hi, May I have one more question?
If I define arr2 at variable panel instead of the next syntax,

Assign activity → arr2 = New Integer(6) {}

Should I change arr2 argument direction from “in/Out” to “Out”?

Hi @gorby

If you want arr2 argument direction to be out change the code like below in Invoke Code:

arr2 = New Integer(6) {}
Array.Copy(arr1, 6, arr2, 0, 7)

Remove that Assign activity and do not define it in variables Panel. Give the argument as out for arr2.

Regards

Hi I do not understand what is “that Assign activity”.
Pls specify this activity.

Hi @gorby

=> Use below syntax in Assign activity to intializa the array

Assign activity -> arr1 = New Integer() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

arr1 of DataType Array(System.Int32)

=> Use the below code in Invoke Code activity

arr2 = New Integer(6) {}
Array.Copy(arr1, 6, arr2, 0, 7)

Invoke Code Arguments:

=> Message Box: String.Join(", ", arr2)

Regards

Hi I cannot understand why I should not define it in variable panel…
I should define it in variable panel because I have to put it in value in argument panel.(See the attached picture red circle. Sorry but variable name is bit different from arr2…)

こんにちは

Array.Copy(arr1, 5, arr2, 0, 7)

InvokeMethodアクティビティ(メソッド呼び出しアクティビティ)で対応可能です

またarr2がarr1からのコピーしたデータのみ(既存データがない)場合は以下の式の方が簡単に対応できます。

arr2 = arr1.Skip(5).Take(7).ToArray()

image

サンプル(Main.xaml と Sequence.xamlを参照ください)

Sample20240519-2a.zip (4.3 KB)

Hi @gorby

arr_ACME_System1_Int32 ahould be in variables Panel only. Since you are copying the elements from one array to another array. The array to which the elements are copied should be initialized. If you define it in variables Panel the argument shoul be In/Out since elements are getting copied to a initialized Array. If you intiialize it in Invoke Code then the argument should be Out.

Regards

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.