StrJoin bulitin function

Propose new features and changes
guest3456
Posts: 2313
Joined: 09 Oct 2013, 10:31

StrJoin bulitin function

20 Dec 2016, 23:12

this was discussed already here with some implementations:
https://autohotkey.com/boards/viewtopic.php?t=7124

Coco wrote:Unfortunately, there is no built-in Join() function. I would love for StrSplit() to have a counterpart.


seems reasonable to have a match for StrSplit() ? either as StrJoin() or an object builtin method

sancarn
Posts: 209
Joined: 01 Mar 2016, 14:52

Re: StrJoin bulitin function

26 Dec 2016, 20:13

+1 Given that they are paired in many languages but not in AHK.
HotKeyIt
Posts: 1645
Joined: 29 Sep 2013, 18:35
Contact:

Re: StrJoin bulitin function

28 Dec 2016, 04:03

Why build-in, it is very simple to do an ahk function and it is very rare required?

Code: [Select all] [Download] GeSHi © Codebox Plus

string:="AHK"
obj:=StrSplit(string)
MsgBox % StrJoin(obj) "`n" StrJoin(obj,"`n")
StrJoin(obj,delimiter:="",OmitChars:=""){
string:=obj[1]
Loop % obj.MaxIndex()-1
string .= delimiter Trim(obj[A_Index+1],OmitChars)
return string
}


EDIT:
Added Trim(..,OmitChars)
sancarn
Posts: 209
Joined: 01 Mar 2016, 14:52

Re: StrJoin bulitin function

30 Dec 2016, 05:40

HotKeyIt wrote:Why build-in, it is very simple to do an ahk function and it is very rare required?

Code: [Select all] [Download] GeSHi © Codebox Plus

string:="AHK"
obj:=StrSplit(string)
MsgBox % StrJoin(obj) "`n" StrJoin(obj,"`n")
StrJoin(obj,delimiter:="",OmitChars:=""){
string:=obj[1]
Loop % obj.MaxIndex()-1
string .= delimiter Trim(obj[A_Index+1],OmitChars)
return string
}


EDIT:
Added Trim(..,OmitChars)


For an intermediate user - a programmer - the problem is trivial to overcome, yes.
But, in my opinion, it is so simple that it should be it's own function... And although it may only be used by 1 person 1 to 2 times, it's used by many more people than something such as COM Objects...

To falsify this go to stack overflow and try the following queries:

"join list" - 56,164 results

"Join String" - 53,056 results

"join array" - 35,345 results

"COM Object" - 25,443 results

Of course StrJoin has many other aliases so to get an exact number of results is quite difficult. COM Object on the other hand is a very broad term... In general however it can be shown that StrJoin is queried about much more often than COM Objects in general... So why build COM Objects into AHK? Especially since community members had already created COM.AHK.

I know COM is vastly more complicated than strjoin, as a function, but it is also significantly less used than very basic operations such as StrJoin. So I don't see how the rarity of use of StrJoin can be used to justify the unimplementation of such a basic function to the AHK language.
User avatar
nnnik
Posts: 2908
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: StrJoin bulitin function

02 Jan 2017, 02:09

You are wrong on several aspects:
A: It is not a lot more used, there are just a lot more questions asked about it on Stack Overflow ( I'm 100% certain that COM Objects are used more often )
B: join string join array etc.. is a general term and may refer to almost anything including COM Objects aside from your function ( Where as COM may only refer to COM )
C: COM Objects are not functions it is an object. The functions in COM.ahk did not fulfill what COM really asked for and were a poor implementation ( That's why it was redone as soon as objects were added )
D: It is not exactly simple. StrSplit causes a loss of information. How that information should be reconstructed differs on a case to case basis and is therefore up to the programmer to implement.
Recommends AHK Studio
sancarn
Posts: 209
Joined: 01 Mar 2016, 14:52

Re: StrJoin bulitin function

02 Jan 2017, 17:03

nnnik wrote:You are wrong on several aspects:
A: It is not a lot more used, there are just a lot more questions asked about it on Stack Overflow ( I'm 100% certain that COM Objects are used more often )


Well if you have better and more accurate "usage statistics" then be my guest to post them... The StackOverflow questions give a "hint" towards what the real numbers are, as I said. A better way, rather, would be to take a commercial application and search for uses of StrJoin and usage of ComObjCreate() (or equivalent). I am 100% sure that COM Objects aren't used as often as StrJoin equivalents in commercial software (and thus generally in programming). I can imagine they are used for setting up APIs/Automation Opportunities and increasing interoperability with other software, but, as I understand it, application's programmer's generally write (or are instructed to write) self-contained code (in the end, most users use the software IN the software itself) and don't rely on external applications (where they can benefit from COM).

nnnik wrote:B: join string join array etc.. is a general term and may refer to almost anything including COM Objects aside from your function ( Where as COM may only refer to COM )


Where COM Objects and StrJoin are included together both should appear in the search results, one would assume. But again, they're very rough figures, as I previously said.

nnnik wrote:C: COM Objects are not functions it is an object. The functions in COM.ahk did not fulfill what COM really asked for and were a poor implementation ( That's why it was redone as soon as objects were added )


I know what a COM Object is... And I know it isn't a function, and never said it was? I was claiming the functions to create such a COM object were vastly more complicated than the function used to join an array.

nnnik wrote:D: It is not exactly simple. StrSplit causes a loss of information. How that information should be reconstructed differs on a case to case basis and is therefore up to the programmer to implement.


StrSplit causes a loss of information. Yes. That loss of information is chosen by the programmer.

Similarly, StrJoin causes a gain of information. That gain of information is chosen by the programmer.

Hence the delimiter argument. What's your point?
User avatar
nnnik
Posts: 2908
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: StrJoin bulitin function

03 Jan 2017, 05:20

sancarn wrote:
nnnik wrote:You are wrong on several aspects:
A: It is not a lot more used, there are just a lot more questions asked about it on Stack Overflow ( I'm 100% certain that COM Objects are used more often )


Well if you have better and more accurate "usage statistics" then be my guest to post them... The StackOverflow questions give a "hint" towards what the real numbers are, as I said. A better way, rather, would be to take a commercial application and search for uses of StrJoin and usage of ComObjCreate() (or equivalent). I am 100% sure that COM Objects aren't used as often as StrJoin equivalents in commercial software (and thus generally in programming). I can imagine they are used for setting up APIs/Automation Opportunities and increasing interoperability with other software, but, as I understand it, application's programmer's generally write (or are instructed to write) self-contained code (in the end, most users use the software IN the software itself) and don't rely on external applications (where they can benefit from COM).
We don't care about general programming. This is AutoHotkey meant for automating other applications and thats what COM-Objects are made for.

sancarn wrote:
nnnik wrote:B: join string join array etc.. is a general term and may refer to almost anything including COM Objects aside from your function ( Where as COM may only refer to COM )


Where COM Objects and StrJoin are included together both should appear in the search results, one would assume. But again, they're very rough figures, as I previously said.
You didn't understand what I meant here and I'm at a loss at how to explain it.

sancarn wrote:
nnnik wrote:C: COM Objects are not functions it is an object. The functions in COM.ahk did not fulfill what COM really asked for and were a poor implementation ( That's why it was redone as soon as objects were added )


I know what a COM Object is... And I know it isn't a function, and never said it was? I was claiming the functions to create such a COM object were vastly more complicated than the function used to join an array.
I just wanted to make clear why COM Objects where built in to AutoHotkey when AutoHotkey_L came out

nnnik wrote:D: It is not exactly simple. StrSplit causes a loss of information. How that information should be reconstructed differs on a case to case basis and is therefore up to the programmer to implement.

StrSplit causes a loss of information. Yes. That loss of information is chosen by the programmer.
Similarly, StrJoin causes a gain of information. That gain of information is chosen by the programmer.
Hence the delimiter argument. What's your point?[/quote]Whether the programmer is able to choose how the information should be gained again depends on how exactly the function looks.
Imagine using StrSplit to parse an HTML document and now you want to redo that by using StrJoin. StrSplit(str, ["<","<\>"], "`n" )
How would any simple StrJoin function be able to regain that information? It it has to be a lot more complex to let the user describe how that information should be regained.
And here we are at the core of the problem. Gain of information happens when you merge already existing information with some kind of context.
For simple StrSplits e.g. ( StrSplit( Str ) || StrSplit( Str, "SplitChar" ) ) the context would be that the StrJoin function should join without adding anything or with adding "SplitChar" between each border.
However as soon as there are more than one SplitChar the StrJoin function will have troubles recreating that.
Maybe you could extend the function with something similar to a Format String or maybe have it process something like an analyzer function.
However all that just won't cover all cases of StrSplit. There simply is no single simple counterpart for StrSplit.
Recommends AHK Studio
sancarn
Posts: 209
Joined: 01 Mar 2016, 14:52

Re: StrJoin bulitin function

05 Jan 2017, 08:11

nnnik wrote:Imagine using StrSplit to parse an HTML document and now you want to redo that by using StrJoin. StrSplit(str, ["<","<\>"], "`n" )
How would any simple StrJoin function be able to regain that information? It it has to be a lot more complex to let the user describe how that information should be regained.
And here we are at the core of the problem. Gain of information happens when you merge already existing information with some kind of context.
For simple StrSplits e.g. ( StrSplit( Str ) || StrSplit( Str, "SplitChar" ) ) the context would be that the StrJoin function should join without adding anything or with adding "SplitChar" between each border.
However as soon as there are more than one SplitChar the StrJoin function will have troubles recreating that.
Maybe you could extend the function with something similar to a Format String or maybe have it process something like an analyzer function.
However all that just won't cover all cases of StrSplit. There simply is no single simple counterpart for StrSplit.


Well I'm not sure if you're wondering how a developer would implement such a function or arguing that "People may assume it works in a way, in which it isn't documented". If the latter, the same can be said about using StrReplace on an array to do the equivalent of:

Code: [Select all] [Download] GeSHi © Codebox Plus

loop, % array.length()
{
StrReplace, array[A_Index] := StrReplace(array[A_Index],_1,_2)
}


It looks like it could work, but it isn't documented to be an intended feature, and thus probably won't work. All that side can be ironed out with documentation.

If the former is true however, all instances of StrJoin that I have seen in the past have been implemented like: StrJoin(arr,delim="") with a strict 1-to-1 joining process. But it's entirely subjective.
lexikos
Posts: 5939
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: StrJoin bulitin function

14 Apr 2017, 00:52

So if StrJoin() was implemented as a built-in function, what would the parameters be?

PHP has join(delimiter, array).
.NET has String.Join(delimiter, array) and String.Join(delimiter, a, b, c, ...).
Java 8 has String.join(delimiter, array) and String.join(delimiter, a, b, c, ...).
Perl has join($delimiter, @array) or join($delimiter, $a, $b, $c, ...).

VBScript has Join(array, delimiter).
Lua has table.concat(array, delimiter).

JavaScript and Ruby have array.join(delimiter).

Python has delimiter.join(array).

Some of them allow the delimiter to be omitted.

(array and delimiter are just pseudo-variable names and may not be appropriate for every language.)

HotKeyIt: I see no reason to include an OmitChars parameter. If StrJoin is the inverse of StrSplit, that should be PadChars. :P
User avatar
nnnik
Posts: 2908
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: StrJoin bulitin function

14 Apr 2017, 01:16

sancarn wrote:

Code: [Select all] [Download] GeSHi © Codebox Plus

loop, % array.length()
{
StrReplace, array[A_Index] := StrReplace(array[A_Index],_1,_2)
}


It looks like it could work, but it isn't documented to be an intended feature, and thus probably won't work. All that side can be ironed out with documentation.
I've read this and wanted to answer but it seems I forgot about this
Doesn't look like anything that could work to me. It is completely illogical - nonsense. What are you trying to do here? You use a StrReplace and output it to the field of an Array but you have not specified what you want to replace or even which string is the source, specifically so basically you have an invalid operation. The second thing ( that is really not completely obvious ) is that the target of a StrReplace command may not be an expression. The third thing is that you have a fe variables that you never specified.

The second thing is that my point never was that it might be misunderstood. My point was that StrJoin isn't the opposite of StrSplit as we have it, but rather a function added only to join Arrays - which is pointless as only the most basic things will be covered and it is 3 lines to write such code. That code might even be made more flexible or parsed against another source of information to construct something meaningful.

Code: [Select all] [Download] GeSHi © Codebox Plus

OutStr := ""
For each, str in Arr
OutStr .= str . "`n"
Recommends AHK Studio
sancarn
Posts: 209
Joined: 01 Mar 2016, 14:52

Re: StrJoin bulitin function

14 Apr 2017, 08:10

lexikos wrote:So if StrJoin() was implemented as a built-in function, what would the parameters be?
VBScript has Join(array, delimiter).
Lua has table.concat(array, delimiter).

JavaScript and Ruby have array.join(delimiter).


These would be my own preferences. I would suggest that StrJoin() should probably follow the same convention as StrSplit though, String := StrJoin(Array [, Delimiters=","]). So similar to VBScript

nnnik wrote:Doesn't look like anything that could work to me.


Lol... It was pseudocode... My point was that it could be assumed that strreplace could take an array as an input parameter and then act on the whole array. But those assumptions can be overcome with documentation.

Your second point, that it's easy to write code for - It may be easy for us as we have had a lot of experience with working with code and such. But for the general population? I still think the strjoin function would be valuable myself. But each is entitled to their own opinion of course :)
User avatar
nnnik
Posts: 2908
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: StrJoin bulitin function

14 Apr 2017, 12:31

sancarn,
If you want arrays and objects to be easier we are going to need a lot more functions than just this one.
Last edited by lexikos on 14 Apr 2017, 19:21, edited 1 time in total.
Reason: Removed the confusing broken quote
Recommends AHK Studio
lexikos
Posts: 5939
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: StrJoin bulitin function

14 Apr 2017, 19:39

sancarn wrote:I would suggest that StrJoin() should probably follow the same convention as StrSplit though, String := StrJoin(Array [, Delimiters=","]).
Thanks for bringing it up. The downside is that there would be no intuitive way to directly pass a list of strings to join (without having to construct an array). It is a string function after all, not an array function.

In theory it could allow both StrJoin(array, delimiter) and StrJoin(delimiter, a, b, c, ...), but then the purpose becomes less obvious if the author hasn't used descriptive variable names. The delimiter could be placed at the end of the list, but it seems less intuitive, and it also prohibits variadic calls like StrJoin(delimiter, a, b, c, the_rest*) (on the other hand, the function could allow multiple arrays of strings to be passed as values, but that's getting more complicated).
My point was that it could be assumed that strreplace could take an array as an input parameter and then act on the whole array.
People can assume all sorts of silly things. Anyway, your pseudo-code example did a very poor job of demonstrating your point. You explicitly retrieve a value from the array with the expression array[A_Index] and store the result in the same array element. In other words, StrReplace is clearly not operating on "the whole array", merely each value that you pass it, one at a time. So it seemed that aside from the syntax errors, what you were demonstrating is perfectly valid (when the array element contains a string).

You should put more thought and care into your pseudo-code, or write real code.
sancarn
Posts: 209
Joined: 01 Mar 2016, 14:52

Re: StrJoin bulitin function

15 Apr 2017, 14:23

lexikos wrote:So it seemed that aside from the syntax errors, what you were demonstrating is perfectly valid (when the array element contains a string).


Perhaps it was a misunderstanding then. Rewritten I was trying to say:

"One might assume that StrReplace can act on an array like this: <insert pseudocode>; but those assumptions can be ironed out with documentation."
joefiesta
Posts: 141
Joined: 24 Jan 2016, 13:54

Re: StrJoin bulitin function

20 Feb 2018, 10:44

I just had this problem and posted a query "does strsplit have a converse?"

I believe the answer should be NO NEW FUNCTION. The solution for simple cases is so trivial that a new command/function would, IMHO, be silly.

HOWEVER !!! I think what SHOULD BE DONE is to post the very simple "converse" to StrSplit in the doc at StrSplit. I'm talking the trivial code:

Code: [Select all] [Download] GeSHi © Codebox Plus

      for kk,value in myarray             ; loop, getting each KKth item, storing in VALUE
mystring .= value . "`n"


Finding this trivial answer took me a very long time.
User avatar
jeeswg
Posts: 4506
Joined: 19 Dec 2016, 01:58
Location: UK

Re: StrJoin bulitin function

20 Feb 2018, 12:45

- @joefiesta: Ideally you would also use VarSetCapacity, and a not-currently-available ObjCount function, unless it's a very small array, to improve performance. Complications to do with VarSetCapacity is one reason I think that both StrRept and StrJoin functions should be implemented. (Other reasons are: that some advanced/very handy one-line logic would be made possible, I benefit from custom versions of these functions all the time, some AHK v2 functions return arrays that then need to be joined as strings.)

- I would suppose/hope that a StrJoin function would join values in numeric keys from 1 to Length(), including any omitted/object keys as blank strings. Your example handles an associative array, which probably wouldn't be handled by a StrJoin function. So it would be a good candidate for inclusion on a StrJoin documentation page.

- I have an example StrJoin function, with a very simple implementation that can handle alternating pad characters. E.g. StrJoin(["`t","`t","`t","`r`n"], oArray*)
StrUnused prototype function: find unused characters for use as delimiters/separators - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=43623&p=198312#p198312

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 3 guests