# Thread: Function to clear array elements

1. ## Function to clear array elements

Hello,

My goal is to create a function to clear column data ('old value' and 'new value') from any array based on array name.
This is a bit hard to explain although I'll try my best.

Array 1
- three columns per element (control name, old value, new value)
ex.
Code:
```public arr1(36,3)
arr1(1,1) = 'txtName'
arr1(1,2) = 'test'
arr1(1,3) = 'testing'```

Array 2
- two columns per element (array name, number of rows in array)
ex.
Code:
```dimension arr2(15,2)
arr2(1,1) = arr1
arr2(1,2) = alen(arr1,1)```
Here is the format I was thinking of while trying to minimize / re-use code:
Code:
```local i = 0
local j = 0

// loop through second array.
for i = 1 to 15
// loop through first array rows and clear columns 2 and 3 (column 1 should not be altered).
for j = 1 to arr2(i,2)
// output should be similar to arr1(1,2) = .F.
// get array name and add element position.
arr2(i,1)(j,2) = .F.
arr2(i,1)(j,3) = .F.
next
next```
I'm possibly approaching this wrong and unsure how to proceed.
How do I fix the inner for loop to have the array name following by the position?

Thanks,
Cory

2. Hi Cory,

If I'm understanding what you're asking you could pass the array name and macro substitute it where necessary. Something like this, but with the looping:

Code:
```public arr1(5,3)
arr1(1,1) = 'txtName'
arr1(1,2) = 'test'
arr1(1,3) = 'testing'
? arr1

proc myproc
para myarray
&myarray(1,2) = .f.
&myarray(1,3) = .f.
? arr1
endproc

do myproc with "arr1"```

3. Hi Dave,

Thanks for helping me out.
I noticed that I was essentially saving a copy of the entire array instead of the array name when I used:
Code:
```arr2(1,1) = arr1
// should be arr2(1,1) = "arr1"```
After a few quick tests and some modification, here is what seems to work:

Code:
```myproc(arr2(1,1), arr2(1,2))

proc myproc(myarray,rowCount)
i = 0
for i = 1 to rowCount
&myarray(&i,2) = .f.
&myarray(&i,3) = .f.
next
endproc```
I still have more testing and modifications but results are now more to what I was hoping for.
Thanks again.

Cory

4. Hi Cory,
After I finished my solution, then find Dave already did it. Anyway, with your idea of having array of array names, here it is:
With my test data to test it and get results:
Code:
```*first array with 2 controls
PUBLIC arrofsection1(2,3)
arrofsection1(1,1)='txtName'
arrofsection1(1,2)='test'
arrofsection1(1,3)='testing'
arrofsection1(2,1)='btnName'
arrofsection1(2,2)='click'
arrofsection1(2,3)='rightclick'

* second array with 1 control
PUBLIC arrofsection2(1,3)
arrofsection2(1,1)='txtSecond'
arrofsection2(1,2)='good'

*third array with 3 controls
PUBLIC arrofsection3(3,3)
arrofsection3(1,1)='txtThird'
arrofsection3(1,2)='3'
arrofsection3(1,3)='33'
arrofsection3(2,1)='chkNew'
arrofsection3(2,2)='boo'
arrofsection3(2,3)='bee'
arrofsection3(3,1)='grdSecond'
arrofsection3(3,2)='any'
arrofsection3(3,3)='noone'

* array of array names
DIMENSION arrnames(3)
arrnames(1)='arrofsection1'
arrnames(2)='arrofsection2'
arrnames(3)='arrofsection3'

i = 0
j = 0
k = 0
*SET STEP ON
for i = 1 to ALEN(arrnames,1)
nwhicharray=arrnames(i)
for j = 1 to ALEN(&nwhicharray,1)
FOR k=1 TO j
&nwhicharray(k,2) = .F.
&nwhicharray(k,3) = .F.
endfor
endfor
ENDFOR
?arrofsection1
?arrofsection2
?arrofsection3```
Somehow I think you are trying to deal with sections controls. Am I right?
Better to explain what for it is and get another and easier solution of your main problem.

Result:
Code:
```Array (refcnt=1)
(
[1,1] = txtName
[1,2] = False
[1,3] = False
[2,1] = btnName
[2,2] = False
[2,3] = False
)

Array (refcnt=1)
(
[1,1] = txtSecond
[1,2] = False
[1,3] = False
)

Array (refcnt=1)
(
[1,1] = txtThird
[1,2] = False
[1,3] = False
[2,1] = chkNew
[2,2] = False
[2,3] = False
[3,1] = grdSecond
[3,2] = False
[3,3] = False
)```

5. Hi Josip,

Thanks for taking the time to figure this out.
Your code almost duplicates my original attempts and gets the results I was looking for.

Thank you.

Cory

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•