0

How do I use C math functions in JQ that take more than one argument? There are no examples in the manual. All it says is:

C math functions that take a single input argument (e.g., sin()) are available as zero-argument jq functions. C math functions that take two input arguments (e.g., pow()) are available as two-argument jq functions that ignore . C math functions that take three input arguments are available as three-argument jq functions that ignore .

I've figured out how to single input argument functions, which are implemented as zero-argument jq functions:

> echo '{"a": 10.12}' | jq '.a | floor' 
10

How do I use something like pow?

What does the manual mean by "jq functions that ignore ."? Do they ignore what's piped in, in the sense that they don't take it as an argument, in contrast to the one input argument case where the argument is taken just from the pipe?

peak
  • 105,803
  • 17
  • 152
  • 177
Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55

1 Answers1

3

It's simple, just separate function arguments by ;:

> echo '{"a": 10.12}' | jq '. | pow(.a;.a)' 
20051775181.748566

Regarding the meaning of jq functions that ignore .: It appears that in the case of single-input-argument functions, like floor, what actually happens is that the default argument . is used, thus no need to actually mention any argument.

In the case of 2 or more input arguments, that of course doesn't do anymore, so no default argument is applied and both argument have to be explicitly passed.

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
  • 1
    Since `.` is identity or the default argument, if you need two parameters, you have to explicitly specify them. It won't just use `.`. – F Trias Apr 23 '20 at 15:19
  • 1
    Perhaps 'that ignore ,' means those functions ignore the current context, and perform their processing based solely on incoming arguments. Sort of like static methods which don't take or ignore 'this'. (I too have been confused by the docs and their usage of '.' in some places) – Bean Taxi Apr 23 '20 at 15:19
  • 1
    From reading the docs, examples for a number of other functions (any, while, until, etc etc) indicate that jq functions use ; to separate parameters instead of , presumably because , is an operator already. However I did not see this explicitly documented anywhere and I certainly didn't know it myself till now. – Bean Taxi Apr 23 '20 at 16:23