Skip to content

Instantly share code, notes, and snippets.

@NikosAlexandris
Created September 10, 2021 13:10
Show Gist options
  • Save NikosAlexandris/8895cd5ee1004667494f05f6590ef6e3 to your computer and use it in GitHub Desktop.
Save NikosAlexandris/8895cd5ee1004667494f05f6590ef6e3 to your computer and use it in GitHub Desktop.
Examples of for loops with Miller

Examples using for

Simple

  • Print
for (k, v in $*) {
  print "KEY IS ". k . " VALUE IS ". v;
}
print

in one line

echo -e 'a,b,c\n1,2,3\n4,5,6' |mlr --csv put -q 'for (k, v in $*) { print "KEY IS ". k . " VALUE IS ". v; } print'

Source: https://johnkerl.org/miller6/programming-language/

  • Print again!
mlr --from f.dat put -q 'for (k, v in $*) { print k . " => " . v }'

Source: https://johnkerl.org/miller6/reference-dsl-variables/#keywords-for-filter-and-put

  • 1
for (k, v in $*) {
  @output_record[k][NR] = v;
}
end {
  emit @output_record
}

in one line

for (k, v in $*) { @output_record[k][NR] = v; } end { emit @output_record}
  • 2
# Find array length
n = 0;
for (k, v in $*) {
  n = max(n, length(v));
}
keys = keys($*);

# Emit one record per array entry
for (int i = 1; i <= n; i+=1) {
  map output_record = {};
  for (k in keys) {
    output_record[k] = $[k][i];
  }
  emit output_record;
}

Source: johnkerl/miller#392 (comment)

  • Unempty
$ cat input.csv
col1,col2,col3
,9,0
8,,
9,,3
for (k,v in $*) {
    if (v == "") {
        $[k] = "NA"
    }
}

in one line

echo -e 'col1,col2,col3\n,9,0\n8,,\n9,,3' |mlr --icsv --otsv put 'for (k,v in $*) { if (v == "") { $[k] = "NA" } }'

Advanced

mlr --icsv --ojson --from mtcars.csv cut -f mpg,wt then put -q '
  for (k, v in $*) {
    @output_record[k][NR] = v;
  }
  end {
    emit @output_record
  }
'

in one line

mlr --icsv --ojson --from mtcars.csv cut -f mpg,wt then put -q 'for (k, v in $*) { @output_record[k][NR] = v; } end { emit @output_record }'

Source: johnkerl/miller#392 (comment)

  • Iterate over data using DSL expressions
mlr --from estimates.tbl put '

  for (k,v in $*) {
    if (is_numeric(v) && k =~ "^[t-z].*$") {
      $sum += v; $count += 1
    }
  }
  $mean = $sum / $count # no assignment if count unset
'

in one line

mlr --from estimates.tbl put 'for (k,v in $*) { if (is_numeric(v) && k =~ "^[t-z].*$") { $sum += v; $count += 1 } } $mean = $sum / $count'

Source: https://johnkerl.org/miller6/misc-examples/

  • Another example
mlr --oxtab stats1 --fr '[i-z]' -a p25,p75 \
    then put 'for (k,v in $*) {
      if (k =~ "(.*)_p25") {
        $["\1_iqr"] = $["\1_p75"] - $["\1_p25"]
      }
    }' \
    data/medium

in one line

mlr --oxtab stats1 --fr '[i-z]' -a p25,p75 then put 'for (k,v in $*) { if (k =~ "(.*)_p25") { $["\1_iqr"] = $["\1_p75"] - $["\1_p25"] } }' data/medium

Source: https://johnkerl.org/miller6/statistics-examples/

  • Check for
mlr --inidx --ifs '\t' --ocsv put -S 'counter=1; for (k,v in $*) {if (v =~ "Ye"){$[k]=v.counter;counter += 1;}}' then cat  input | tail -n +2 | mlr --c2p cat

in johnkerl/miller#225 (comment)

Check for Markdown example

$ cat to-tsv.mlr
NR == 1 {
    printn "| ";
    for (k, v in $*) {
        printn string(k) . " |";
    }
    print "";
    printn "|";
    for (k, v in $*) {
        printn " ---- |";
    }
    print "";
}
printn "| ";
for (k, v in $*) {
    printn string(v) . " |";
}
print "";

Source: johnkerl/miller#106 (comment)

Advanced 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment