Reference: The POSIX standard for the
One thing I really miss in
awk is the ability to join an array with a delimiter, like with the
join command in Perl, typically done for immediate output.
Instead, I end up with writing code like
for (key in array) joined_string = (joined_string == "" ? array[key] : joined_string "," array[key]) print joined_string
joined_string = array for (i = 2; i <= length(array); ++i) joined_string = joined_string "," array[i]; print joined_string
awk may do this for me if I change the current fields:
OFS="," # (would probably do this in BEGIN) n = 0 for (key in array) $(++n) = array[key] print
I believe that this is perfectly legal. However, this will produce garbage in the output if the current input record has more fields than the
array array has entries (the "garbage" would be data from the input file). It would therefore be nice to be able to do
OFS = "," # (would probably do this in BEGIN) n = 0 for (key in array) $(++n) = array[key] NF = n print
I can't find any text in the standard saying that modifying
NF is allowed, but neither any text saying that it's not allowed or that it invokes undefined behaviour. The information I can find is that
NF. This does not say that I'm not allowed to write my own function or code block that resets
NF, and it gives a precedence for doing this with the existence of the
It's also stated that assigning to
$0 is allowed and that this resets
NF. Does that mean that the following code would be better?
OFS = "," # (would probably do this in BEGIN) $0 = "" n = 0 for (key in array) $(++n) = array[key] print
- Is setting
- Would that last piece of code be the correct way of joining an array with a delimiter for output?