cookbook iterate

This commit is contained in:
John Kerl 2017-02-20 23:07:03 -05:00
parent 814a69d60f
commit 75c649fbd7
4 changed files with 101 additions and 7 deletions

View file

@ -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:

View file

@ -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
</div>
<!-- ================================================================ -->
<h1>Splitting nested fields</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_splitting_nested');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_splitting_nested" style="display: block">
<p/> Suppose you have a TSV file like this:
POKI_INCLUDE_ESCAPED(data/nested.tsv)HERE
<p/> One option to split out the colon-delimited values in the <tt>b</tt>
column is to use <tt>splitnv</tt> 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
<p/> while another is to loop over the same map from <tt>splitnv</tt> and use
it (with <tt>put -q</tt> 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
</div>
<!-- ================================================================ -->
<h1>Finding missing dates</h1>

View file

@ -196,6 +196,7 @@ Miller commands were run with pretty-print-tabular output format.
&bull;&nbsp;<a href="#Full_field_renames_and_reassigns">Full field renames and reassigns</a><br/>
&bull;&nbsp;<a href="#Numbering_and_renumbering_records">Numbering and renumbering records</a><br/>
&bull;&nbsp;<a href="#Data-cleaning_examples">Data-cleaning examples</a><br/>
&bull;&nbsp;<a href="#Splitting_nested_fields">Splitting nested fields</a><br/>
&bull;&nbsp;<a href="#Finding_missing_dates">Finding missing dates</a><br/>
&bull;&nbsp;<a href="#Two-pass_algorithms">Two-pass algorithms</a><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&bull;&nbsp;<a href="#Two-pass_algorithms:_computation_of_percentages">Two-pass algorithms: computation of percentages</a><br/>
@ -612,6 +613,80 @@ fred,true
</div>
<p/>
</div>
<!-- ================================================================ -->
<a id="Splitting_nested_fields"/><h1>Splitting nested fields</h1>
<button style="font-weight:bold;color:maroon;border:0" padding=0 onclick="toggle_by_name('section_toggle_splitting_nested');" href="javascript:;">Toggle section visibility</button>
<div id="section_toggle_splitting_nested" style="display: block">
<p/> Suppose you have a TSV file like this:
<p/>
<div class="pokipanel">
<pre>
a b
x z
s u:v:w
</pre>
</div>
<p/>
<p/> One option to split out the colon-delimited values in the <tt>b</tt>
column is to use <tt>splitnv</tt> to create an integer-indexed map and loop
over it, adding new fields to the current record:
<p/>
<div class="pokipanel">
<pre>
$ 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
</pre>
</div>
<p/>
<p/> while another is to loop over the same map from <tt>splitnv</tt> and use
it (with <tt>put -q</tt> to suppress printing the original record) to produce
multiple records:
<p/>
<div class="pokipanel">
<pre>
$ 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
</pre>
</div>
<p/>
<p/>
<div class="pokipanel">
<pre>
$ 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
</pre>
</div>
<p/>
</div>
<!-- ================================================================ -->
<a id="Finding_missing_dates"/><h1>Finding missing dates</h1>

3
doc/data/nested.tsv Normal file
View file

@ -0,0 +1,3 @@
a b
x z
s u:v:w
1 a b
2 x z
3 s u:v:w