| View previous topic :: View next topic |
| Author |
Message |
Rabiator
Joined: 17 Apr 2005 Posts: 289 Location: Sauerland
|
Posted: Wed Feb 11, 2009 11:04 pm Post subject: StringJoin() |
|
|
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  |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Fri Feb 13, 2009 5:59 am Post subject: |
|
|
| 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 |
|
|
| Back to top |
|
 |
Rabiator
Joined: 17 Apr 2005 Posts: 289 Location: Sauerland
|
Posted: Fri Feb 13, 2009 7:57 pm Post subject: |
|
|
Thank you for this hint. I had it before my very eyes but didn't read it ... .
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. |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Fri Aug 28, 2009 10:18 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Fri Aug 28, 2009 11:12 am Post subject: |
|
|
| 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  |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Fri Aug 28, 2009 11:17 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Fri Aug 28, 2009 11:29 am Post subject: |
|
|
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  |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Fri Aug 28, 2009 11:42 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Fri Aug 28, 2009 7:11 pm Post subject: |
|
|
| 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  |
|
| Back to top |
|
 |
Icarus
Joined: 24 Nov 2005 Posts: 851
|
Posted: Fri Aug 28, 2009 7:15 pm Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
|