3 ms·
Yes, if the "destination" is stdout (or a pipe/file) then the obvious loop works just fine: function join(ARRAY) { for (i=0; i in ARRAY; i++) printf "%s"
by kiwidrew 8y ago
Yes, if the "destination" is stdout (or a pipe/file) then the obvious loop works just fine:
function join(ARRAY) {
for (i=0; i in ARRAY; i++) printf "%s", ARRAY[i];
}
But if you need the result back as a string for further processing, the obvious methods are not linear:
function join(ARRAY,_s) {
for (i=0; i in ARRAY; i++) _s=_s ARRAY[i];
return _s;
}
# try to be clever and join 2 items at the same time
function join2(ARRAY,_s) {
for (i=0; i in ARRAY; i+=2)
_s=sprintf("%s%s%s",_s,ARRAY[i],ARRAY[i+1]);
return _s;
}
I for one definitely miss having a join() function, and it seems odd that this natural complement to the split() function was never implemented...