| View previous topic :: View next topic |
| Author |
Message |
goxu
Joined: 21 May 2009 Posts: 18
|
Posted: Mon Jun 15, 2009 1:39 am Post subject: Passing Arrays to a function |
|
|
Hi, I'm wondering if it is possible to pass arrays to a custom function.
| Code: | 01: colors = red`nblue`ngreen
02: StringSplit, color_array, colors, `n
03:
04: process_array(color_array)
05:
06: process_array(passed_array)
07: {
08: global
09: MsgBox %passed_array1%
10: ;MsgBox %color_array1%
11: } |
This does not recognize the passed array. I like to avoid calling the direct array name like the line 10. Is there a way to do it? |
|
| Back to top |
|
 |
Krogdor
Joined: 18 Apr 2008 Posts: 1390 Location: The Interwebs
|
Posted: Mon Jun 15, 2009 2:08 am Post subject: |
|
|
| Code: | colors = red`nblue`ngreen
StringSplit, color_array, colors, `n
process_array(color_array)
process_array(passed_array)
{
global
MsgBox % %passed_array%1
;MsgBox %color_array1%
} |
Is that what you want? |
|
| Back to top |
|
 |
goxu
Joined: 21 May 2009 Posts: 18
|
Posted: Mon Jun 15, 2009 2:24 am Post subject: |
|
|
No, I mean I'd like to retrive the array elements in the function.
In this case, I've expected the code above would return 'red' because %passed_array1% corresponds to %color_array1%, but it doesn't. |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Mon Jun 15, 2009 3:26 am Post subject: |
|
|
Pass the name of the array as literal text. The variable color_array has no value. | Code: | colors = red`nblue`ngreen
StringSplit, color_array, colors, `n
process_array("color_array")
return
process_array(passed_array)
{
global
MsgBox % %passed_array%1
;MsgBox %color_array1%
} |
|
|
| Back to top |
|
 |
goxu
Joined: 21 May 2009 Posts: 18
|
Posted: Mon Jun 15, 2009 5:54 am Post subject: |
|
|
thanks jaco, I never imagined it could be done that way.
So, here is my script which checks if a passed array contains repeated elements. Regarding the line 14 and 16, why couldn't they be written in an usual way like %arrayname0% and arrayname%A_Index%?
| Code: | 01: colors = red`nblue`ngreen`nred`n
02: StringSplit, color_array, colors, `n
03:
04: if chk_same_element("color_array")
05: MsgBox True
06: else
07: MsgBox False
08:
09: chk_same_element(arrayname)
10: {
11: global
12: num_chk := %arrayname%0
13: counter := 0
14: Loop, %num_chk% ;1. Loop, %arrayname0% didn't work
15: {
16: array_element%A_Index% := %arrayname%%A_Index% ;2. arrayname%A_Index% didn't work
17: if array_element%A_Index%
18: {
19: counter += 1
20: chked_elements%counter% := array_element%A_Index%
21: }
22: }
23: repeated =
24: Loop, %counter%
25: {
26: chkelement := chked_elements%A_Index%
27: scan_number := A_Index
28: Loop, %counter%
29: {
30: if (A_Index = scan_number)
31: continue
32: else if (chkelement = chked_elements%A_Index%)
33: {
34: repeated := true
35: break
36: }
37: }
38: }
39: if repeated
40: return true
41: else
42: return false
43: } |
|
|
| Back to top |
|
 |
Z_Gecko Guest
|
Posted: Mon Jun 15, 2009 6:18 am Post subject: |
|
|
oh, sorry for the empty post, seems like the forum doesnīt like some links.
to cut it short: you are dereferencing in these lines. check: | Code: | | http://en.wikipedia.org/wiki/Reference_(computer_science) |
and please donīt put line numbers in front of your code, itīs annoing. Thanks |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5044 Location: the tunnel(?=light)
|
|
| Back to top |
|
 |
|