mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-28 10:13:59 +00:00
cookbook iterate
This commit is contained in:
parent
814a69d60f
commit
75c649fbd7
4 changed files with 101 additions and 7 deletions
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -196,6 +196,7 @@ Miller commands were run with pretty-print-tabular output format.
|
|||
• <a href="#Full_field_renames_and_reassigns">Full field renames and reassigns</a><br/>
|
||||
• <a href="#Numbering_and_renumbering_records">Numbering and renumbering records</a><br/>
|
||||
• <a href="#Data-cleaning_examples">Data-cleaning examples</a><br/>
|
||||
• <a href="#Splitting_nested_fields">Splitting nested fields</a><br/>
|
||||
• <a href="#Finding_missing_dates">Finding missing dates</a><br/>
|
||||
• <a href="#Two-pass_algorithms">Two-pass algorithms</a><br/>
|
||||
• <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
3
doc/data/nested.tsv
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a b
|
||||
x z
|
||||
s u:v:w
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue