Soloway's recurring rainfall: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: Added Perl)
Line 506: Line 506:
}
}
}
}
</syntaxhighlight>

=={{header|jq}}==
The following program is mainly intended for interactive use.
An appropriate invocation would be: jq -nrR -f compute-averages.jq
Please note:
* a null response is regarded as an invalid entry;
* if there are no valid entries, the average is not printed;
* leading and trailing spaces in entries are ignored, and a single + or - prefix is allowed;
* an "end-of-file" condition is equivalent to the occurrence of 99999.

<syntaxhighlight lang=jq>
def isinteger: test("^ *[+-]?[0-9]+ *$");

def soloway:
label $out
| foreach (inputs, null) as $x (null;
.invalid = false
| if $x == null then .break = true
elif ($x|isinteger) then ($x|tonumber) as $n
| if $n == 99999
then .break = true
else .sum += $n | .n += 1
end
else .invalid = $x
end;
if .break then ., break $out
elif .invalid then .
else empty
end)
| (select(.invalid) | "Invalid entry (\(.invalid)). Please try again."),
(select(.break and .sum) | "Average is: \(.sum / .n)") ;

soloway
</syntaxhighlight>
</syntaxhighlight>