AutoHotkey Community

It is currently May 26th, 2012, 6:37 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: June 15th, 2009, 2:39 am 
Offline

Joined: May 21st, 2009, 9:44 am
Posts: 18
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2009, 3:08 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2009, 3:24 am 
Offline

Joined: May 21st, 2009, 9:44 am
Posts: 18
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2009, 4:26 am 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
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%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2009, 6:54 am 
Offline

Joined: May 21st, 2009, 9:44 am
Posts: 18
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:   }


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2009, 7:18 am 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2009, 2:03 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: dmg, rbrtryn and 61 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group