AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing Arrays to a function

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
goxu



Joined: 21 May 2009
Posts: 18

PostPosted: Mon Jun 15, 2009 1:39 am    Post subject: Passing Arrays to a function Reply with quote

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
View user's profile Send private message
Krogdor



Joined: 18 Apr 2008
Posts: 1390
Location: The Interwebs

PostPosted: Mon Jun 15, 2009 2:08 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
goxu



Joined: 21 May 2009
Posts: 18

PostPosted: Mon Jun 15, 2009 2:24 am    Post subject: Reply with quote

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
View user's profile Send private message
jaco0646



Joined: 07 Oct 2006
Posts: 3113
Location: MN, USA

PostPosted: Mon Jun 15, 2009 3:26 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
goxu



Joined: 21 May 2009
Posts: 18

PostPosted: Mon Jun 15, 2009 5:54 am    Post subject: Reply with quote

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
View user's profile Send private message
Z_Gecko
Guest





PostPosted: Mon Jun 15, 2009 6:18 am    Post subject: Reply with quote

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)

PostPosted: Mon Jun 15, 2009 1:03 pm    Post subject: Reply with quote

Z_Gecko wrote:
oh, sorry for the empty post, seems like the forum doesnīt like some links.


There was a small pow-wow about this a few months ago, the easiest way to enter the link in and have the forum display it properly is to use %28 and %29 in place of the left and right parenthesis in the URL.

http://en.wikipedia.org/wiki/Reference_%28computer_science%29
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group