From 75c649fbd7679237ab223a1960fc16e78f6ccf08 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Mon, 20 Feb 2017 23:07:03 -0500 Subject: [PATCH] cookbook iterate --- c/todo.txt | 7 ---- doc/content-for-cookbook.html | 23 +++++++++++ doc/cookbook.html | 75 +++++++++++++++++++++++++++++++++++ doc/data/nested.tsv | 3 ++ 4 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 doc/data/nested.tsv diff --git a/c/todo.txt b/c/todo.txt index 4b9ac20cd..365231b26 100644 --- a/c/todo.txt +++ b/c/todo.txt @@ -42,13 +42,6 @@ UT FOR 5.0.0: ? parameter marshaling in single-rhs-assign-to-map-lhs context -================================================================ -COOKBOOK CHECKLIST: - -* make this easier: - mlr --from segv.tsv --itsvlite --oxtab put 'o=splitnv($b, ":"); for (k,v in o) {$["p".k]=v}' - mlr --from segv.tsv --itsvlite --oxtab put -q 'o=splitnv($b, ":"); for (k,v in o) {emit mapsum($*, {"b":v})}' - ================================================================ MAPVAR CHECKLIST: diff --git a/doc/content-for-cookbook.html b/doc/content-for-cookbook.html index 573e3e64d..06eae6aef 100644 --- a/doc/content-for-cookbook.html +++ b/doc/content-for-cookbook.html @@ -130,6 +130,29 @@ POKI_RUN_COMMAND{{mlr --icsv --opprint put 'if (!is_string($reachable)) {eprint POKI_RUN_COMMAND_TOLERATING_ERROR{{mlr --csv put '$reachable = asserting_string($reachable)' data/het-bool.csv}}HERE + + +

Splitting nested fields

+ +
+ +

Suppose you have a TSV file like this: + +POKI_INCLUDE_ESCAPED(data/nested.tsv)HERE + +

One option to split out the colon-delimited values in the b +column is to use splitnv to create an integer-indexed map and loop +over it, adding new fields to the current record: + +POKI_RUN_COMMAND{{mlr --from data/nested.tsv --itsv --oxtab put 'o=splitnv($b, ":"); for (k,v in o) {$["p".k]=v}'}}HERE + +

while another is to loop over the same map from splitnv and use +it (with put -q to suppress printing the original record) to produce +multiple records: + +POKI_RUN_COMMAND{{mlr --from data/nested.tsv --itsv --oxtab put -q 'o=splitnv($b, ":"); for (k,v in o) {emit mapsum($*, {"b":v})}'}}HERE +POKI_RUN_COMMAND{{mlr --from data/nested.tsv --tsv put -q 'o=splitnv($b, ":"); for (k,v in o) {emit mapsum($*, {"b":v})}'}}HERE +

Finding missing dates

diff --git a/doc/cookbook.html b/doc/cookbook.html index 2d568d4b9..9cdffabf9 100644 --- a/doc/cookbook.html +++ b/doc/cookbook.html @@ -196,6 +196,7 @@ Miller commands were run with pretty-print-tabular output format. • Full field renames and reassigns
• Numbering and renumbering records
• Data-cleaning examples
+• Splitting nested fields
• Finding missing dates
• Two-pass algorithms
    • Two-pass algorithms: computation of percentages
@@ -612,6 +613,80 @@ fred,true

+ + +

Splitting nested fields

+ +
+ +

Suppose you have a TSV file like this: + +

+

+
+a	b
+x	z
+s	u:v:w
+
+
+

+ +

One option to split out the colon-delimited values in the b +column is to use splitnv to create an integer-indexed map and loop +over it, adding new fields to the current record: + +

+

+
+$ mlr --from data/nested.tsv --itsv --oxtab put 'o=splitnv($b, ":"); for (k,v in o) {$["p".k]=v}'
+a  x
+b  z
+p1 z
+
+a  s
+b  u:v:w
+p1 u
+p2 v
+p3 w
+
+
+

+ +

while another is to loop over the same map from splitnv and use +it (with put -q to suppress printing the original record) to produce +multiple records: + +

+

+
+$ mlr --from data/nested.tsv --itsv --oxtab put -q 'o=splitnv($b, ":"); for (k,v in o) {emit mapsum($*, {"b":v})}'
+a x
+b z
+
+a s
+b u
+
+a s
+b v
+
+a s
+b w
+
+
+

+

+

+
+$ mlr --from data/nested.tsv --tsv put -q 'o=splitnv($b, ":"); for (k,v in o) {emit mapsum($*, {"b":v})}'
+a	b
+x	z
+s	u
+s	v
+s	w
+
+
+

+

Finding missing dates

diff --git a/doc/data/nested.tsv b/doc/data/nested.tsv new file mode 100644 index 000000000..6130a5b8e --- /dev/null +++ b/doc/data/nested.tsv @@ -0,0 +1,3 @@ +a b +x z +s u:v:w