Kernighans large earthquake problem: Difference between revisions

Content added Content deleted
Line 1,142: Line 1,142:
8/27/1883 Krakatoa 8.8
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
5/18/1980 MountStHelens 7.6
</pre>

=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''

To meet the task requirements in a minimal way, one could invoke
jq as follows:
<pre>
jq -Rrn 'inputs | . as $line | [splits(" *") ] | select((.[2]|tonumber) > 6) | $line' data.txt
</pre>
The program shown in the following subsection, by contrast, determines the file name dynamically and includes
some error-checking. The output shown below is based on the invocation:
<pre>
jq -Rrn -f large-earthquake-problem.jq data.txt
</pre>
where data.txt is as for [[#Snobol|Snobol]].

<lang jq>input as $one
| "The earthquakes from \(input_filename) with a magnitude greater than 6 are:\n",
( $one, inputs
| . as $line
| [splits(" *")]
| if length < 3
then "WARNING: invalid line:\n\($line)"
else try ((.[2] | tonumber) as $mag
| select($mag > 6)
| $line) catch "WARNING: column 3 is not a recognized number in the line:\n\($line)"
end )
</lang>
{{out}}
<pre>
The earthquakes from data.txt with a magnitude greater than 6 are:

8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1
</pre>
</pre>