AutoHotkey Community

It is currently May 26th, 2012, 8:47 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: StringJoin()
PostPosted: February 12th, 2009, 12:04 am 
Offline

Joined: April 17th, 2005, 7:47 pm
Posts: 289
Location: Sauerland
This function does the opposite of StringSplit; it joins strings from an AHK-array with a delimiter, stopping at the first empty array element.
Code:
Var1 := "a"
Var2 := 1.234
Var3 := "b"

MsgBox % StringJoin("Var")

StringJoin(array, delimiter = ";")
{
  Loop
    If Not %array%%A_Index% Or Not t .= (t ? delimiter : "") %array%%A_Index%
      Return t
}

Well, .....
I wasn't sure in which section I should post this. My point is: I wonder why this function works! :)

The first argument is the identifier of the array as text. The function accesses the array elements and joins them. As planned.
However, it shouldn't be able to do this :!:, because there is no Global declaration inside the function nor a local variable is declared (that would supersede the Global declaration).

But it's getting even better :). A code, that accesses directly an array element inside the function will make it invisible for the function. No matter whether the code is executed. In the following example the function returns only 2 elements, because the 3rd element doesn't exist.
Code:
StringJoin(s, d=";")
{
  Loop
    If Not %s%%A_Index% Or Not t .= (t ? d : "") %s%%A_Index%
      Return t
  dummy := Var3  ; This code won't even be executed. It may just as well be written at the top of the function.
}

After some experiments I conclude, that the default localization of function variables only applies to directly accessed variables, but not to variables, which names have to be pieced together before being used. Perhaps a preprocessor has to "hide" all non global variables in each function during the start of the script, but can of course only apply this to non dynamic variables?

I don't know whether this was discussed before, but for me this is not a bug, but a useful feature.
Chris, please don't change this! :)

__________________________________________
Created with BBCodeWriter 7.0 - the one and only :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 6:59 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
AHK Help wrote:
any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists. If neither exists and the usage requires the variable to be created, it is created as a local variable unless the assume-global mode is in effect


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 8:57 pm 
Offline

Joined: April 17th, 2005, 7:47 pm
Posts: 289
Location: Sauerland
Thank you for this hint. I had it before my very eyes but didn't read it ... :oops: .

Anyhow, we have to regard that a possible reference of Array%i% to a global variable is destroyed, if the variable appears in the function with its literal name and without a global declaration.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 11:18 am 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
I am looking for a solution like this also.
This proposed solution will only work on global arrays, so we are still stuck.
Code:
test()
Return

test() {
   ;Global      ; Will only work if this is global
   string := "a,123,b"
   StringSplit array, string, `,         
   joined := StringJoin("array")
   msgbox %joined%
}

StringJoin(array, delimiter = ";")
{
  Loop
    If Not %array%%A_Index% Or Not t .= (t ? delimiter : "") %array%%A_Index%
      Return t
}


I think that until real arrays are implemented in AHK, maybe it would not be such a bad idea to implement StringJoin

_________________
Sector-Seven - Freeware tools built with AutoHotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 12:12 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
Icarus wrote:
I am looking for a solution like this also.
This proposed solution will only work on global arrays, so we are still stuck.


What do you mean by global arrays only?

So you want a function/command that can change a local array of a function?

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 12:17 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
I mean the code that I placed above will not work unless you CALL the join function from a global scope.

_________________
Sector-Seven - Freeware tools built with AutoHotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 12:29 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
So you would like another function to access local variables when called from that function?

The only way I can think of would be to to forward the pointer(s) of a variable(s)
I would suggest to add that functionality as a gosub in your function:
Code:
test()
Return

test() {
   string := "a,123,b"
   StringSplit array, string, `,
   A_string=array
   Gosub, StringJoin
   msgbox %StringJoin%
   Return
   
   StringJoin:
   Loop % %A_String%0
      StringJoin .= (StringJoin<>"" ? ("," . %A_String%%A_Index%) : %A_String%%A_Index%)
   Return
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 12:42 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
No offense, but your code is a mess... :)
A subroutine inside a function?...

Even though it is working, I am sure you realize this is not a solution.
Not only it is not a good practice, it will only work inside that specific function.

The only way this could normally be achieved, without abusing some hidden "features" in AHK, would be if StringJoin is implemented on the native level.

_________________
Sector-Seven - Freeware tools built with AutoHotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 8:11 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
Icarus wrote:
The only way this could normally be achieved, without abusing some hidden "features" in AHK, would be if StringJoin is implemented on the native level.


Don't get me wrong, I am not against this functionality, but I think it is not that easy to program and very unlikely to appear before arrays.

I hope as soon as a true Array is implemented, all commands will be adapted to work with true array or functions will be written that give all existing and new features :)

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2009, 8:15 pm 
Offline

Joined: November 24th, 2005, 8:16 am
Posts: 851
HotKeyIt wrote:
I hope as soon as a true Array is implemented, all commands will be adapted to work with true array or functions will be written that give all existing and new features :)

I'll drink to that :)

_________________
Sector-Seven - Freeware tools built with AutoHotkey


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Blackholyman, RoAltmann and 10 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