{"record":{"id":"cb3d5c894d7205d6","repo":"mikefarah/yq","slug":"reduce-must-be-given-a-variables-assignment-got","errorCode":null,"errorMessage":"reduce must be given a variables assignment, got %v instead","messagePattern":"reduce must be given a variables assignment, got (.+?) instead","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_reduce.go","lineNumber":19,"sourceCode":"package yqlib\n\nimport (\n\t\"container/list\"\n\t\"fmt\"\n)\n\nfunc reduceOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {\n\tlog.Debugf(\"reduceOp\")\n\t//.a as $var reduce (0; . + $var)\n\t//lhs is the assignment operator\n\t//rhs is the reduce block\n\t// '.' refers to the current accumulator, initialised to 0\n\t// $var references a single element from the .a\n\n\t//ensure lhs is actually an assignment\n\t//and rhs is a block (empty)\n\tif expressionNode.LHS.Operation.OperationType != assignVariableOpType {\n\t\treturn Context{}, fmt.Errorf(\"reduce must be given a variables assignment, got %v instead\", expressionNode.LHS.Operation.OperationType.Type)\n\t} else if expressionNode.RHS.Operation.OperationType != blockOpType {\n\t\treturn Context{}, fmt.Errorf(\"reduce must be given a block, got %v instead\", expressionNode.RHS.Operation.OperationType.Type)\n\t}\n\n\tarrayExpNode := expressionNode.LHS.LHS\n\tarray, err := d.GetMatchingNodes(context, arrayExpNode)\n\n\tif err != nil {\n\t\treturn Context{}, err\n\t}\n\n\tvariableName := expressionNode.LHS.RHS.Operation.StringValue\n\n\tinitExp := expressionNode.RHS.LHS\n\n\taccum, err := d.GetMatchingNodes(context, initExp)\n\tif err != nil {\n\t\treturn Context{}, err","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_reduce.go#L1-L37","documentation":"The `reduce` operator requires its expression to have the shape `$var := (...)` — the left-hand side must be a variable assignment operation. This check fires when the LHS operation is something else (a plain expression, comparison, etc.). yq throws it because reduce's accumulator update semantics depend on the variable-assignment form.","triggerScenarios":"`yq 'reduce .a as $x (0; ...)'` is correct, but writing malformed reduce syntax such as `reduce .a (0; . + 1)` or `reduce (.a + 1) as $x (0; ...)` where the LHS is not an assignment; also inner misplacements like `reduce .a = $x (0; ...)` using the wrong assignment operator.","commonSituations":"Translating jq reduce syntax incorrectly; typos dropping the `as $var` clause; older/mistyped examples; confusing `=` with `:=`/`as` assignment forms.","solutions":["Use the full correct form: `reduce <arrayExpr> as $var (<init>; <update>)`.","Ensure the part before `as` is the array expression, not the assignment — the assignment is the whole `as $var` clause.","Compare against working examples in yq docs (e.g. `reduce .[] as $item (0; . + $item)`).","Check operator precedence — parenthesize the array expression if it contains operators."],"exampleFix":"// before\nyq 'reduce .a = $x (0; . + $x)' file.yml\n// after\nyq 'reduce .a[] as $x (0; . + $x)' file.yml","handlingStrategy":"try-catch","validationCode":"echo \"$expr\" | grep -Eq '^reduce .+ as \\\\$[a-zA-Z_]+ \\\\(' || echo \"reduce must use the form: reduce <array> as \\$var (init; update)\"","typeGuard":null,"tryCatchPattern":"out=$(yq \"$expr\" file.yml 2>&1) || { echo \"$out\"; case \"$out\" in *\"variables assignment\"*) echo \"fix reduce LHS: add 'as \\$var'\";; esac; }","preventionTips":["Always use the full form reduce <arrayExpr> as $var (init; update).","Never replace the as-clause with = or :=.","Parenthesize the array expression when it contains operators.","Copy verified examples from the yq reduce documentation."],"tags":["yq","reduce","syntax-error","expression-structure"],"backgroundTag":"reduce-invalid-expression-shape","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}