From: pkoppstein Date: Mon, 6 Oct 2014 18:37:57 +0000 (-0400) Subject: fix sub (#586); add gsub/3; add transpose/0. X-Git-Tag: jq-1.5rc1~60 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=85f0e30c817631c2c76d8b5e35443509283c638e;p=jq fix sub (#586); add gsub/3; add transpose/0. Signed-off-by: William Langford --- diff --git a/builtin.c b/builtin.c index 6f66894..7ca0f81 100644 --- a/builtin.c +++ b/builtin.c @@ -1051,17 +1051,19 @@ static const char* const jq_builtins[] = { "def sub($re; s):" " . as $in" " | [match($re)]" - " | .[0]" - " | . as $r" - // # create the \"capture\" object: - " | reduce ( $r | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair" - " ({}; . + $pair)" - " | if . == {} then $in | .[0:$r.offset]+s+.[$r.offset+$r.length:]" - " else (. | s)" + " | if length == 0 then $in" + " else .[0]" + " | . as $r" + // # create the \"capture\" object: + " | reduce ( $r | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair" + " ({}; . + $pair)" + " | if . == {} then $in | .[0:$r.offset]+s+.[$r.offset+$r.length:]" + " else (. | s)" + " end" " end ;", // // repeated substitution of re (which may contain named captures) - "def gsub($re; s):" + "def gsub($re; s; flags):" // # _stredit(edits;s) - s is the \"to\" string, which might contain capture variables, // # so if an edit contains captures, then create the capture object and pipe it to s " def _stredit(edits; s):" @@ -1076,7 +1078,8 @@ static const char* const jq_builtins[] = { " else (if $l == 0 then \"\" else ($in | _stredit(edits[0:$l]; s)) end) + (. | s)" " end" " end ;" - " [match($re;\"g\")] as $edits | _stredit($edits; s) ;", + " [match($re; flags + \"g\")] as $edits | _stredit($edits; s) ;", + "def gsub($re; s): gsub($re; s; \"\");" //####################################################################### // range/3, with a `by` expression argument @@ -1096,6 +1099,16 @@ static const char* const jq_builtins[] = { "def first: .[0];", "def last: .[-1];", "def nth($n): .[$n];", + // # transpose a possibly jagged matrix, quickly; + // # rows are padded with nulls so the result is always rectangular. + "def transpose:" + " if . == [] then []" + " else . as $in" + " | (map(length) | max) as $max" + " | length as $length" + " | reduce range(0; $max) as $j" + " ([]; . + [reduce range(0;$length) as $i ([]; . + [ $in[$i][$j] ] )] )" + " end;", }; #undef LIBM_DD diff --git a/docs/content/3.manual/manual.yml b/docs/content/3.manual/manual.yml index 16bee1e..a89feb6 100644 --- a/docs/content/3.manual/manual.yml +++ b/docs/content/3.manual/manual.yml @@ -1341,6 +1341,18 @@ sections: input: 'null' output: ['"less"'] + - title: "`transpose`" + body: | + + Transpose a possibly jagged matrix (an array of arrays). + Rows are padded with nulls so the result is always rectangular. + + examples: + - program: 'target' + input: '[[1], [2,3]]' + output: ['[1,2],[null,3]'] + + - title: "String interpolation - `\\(foo)`" body: | diff --git a/tests/all.test b/tests/all.test index 1807890..84d4491 100644 --- a/tests/all.test +++ b/tests/all.test @@ -1094,3 +1094,7 @@ flatten(2) flatten(2) [0, [1, [2]], [1, [[3], 2]]] [0, 1, 2, 1, [3], 2] + +transpose +[[1], [2,3]] +[[1,2],[null,3]]