Search code examples
awk

awk opposite of split


what would be an opposite of split() in awk? Imagine I have array containig characters/integers.

What I've tried:

color = "#FFFF00";
printf("color original: %s\n", color);
split(color, chars, "");
joined = "";
for (i=1; i <= length(chars); i++) {
    joined = joined + chars[i];
}
printf("color joined: %s\n", joined);

however the output is:

color original: #FFFF00
color joined: 0

that is of course incorrect.

UPDATE: cool, ended up with the following code (inspired by join function present in answers):

color = "#FFFF00";
printf("color original: %s\n", color);
split(color, chars, "");
joined = "";
for (i=1; i <= length(chars); i++) {
    joined = joined "" chars[i];
}
printf("color joined: %s\n", joined);

the trick was not to use + sign when joining things back


Solution

  • Knowing that the opposite of split() is join(), a mere Google Search gives me this page, which seems to contain the solution : http://www.gnu.org/software/gawk/manual/html_node/Join-Function.html . It joins all the elements of an array together, and returns the corresponding string.

    ['f','o','o'] => "foo"
    

    Have fun