Kernighans large earthquake problem: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
 
(27 intermediate revisions by 14 users not shown)
Line 1:
{{task}}
 
[https[w://en.wikipedia.org/wiki/Brian_KernighanBrian Kernighan|Brian Kernighan]], in a [https://www.youtube.com/watch?v=Sg4U4r_AgJU lecture] at the University of Nottingham, described a [https://youtu.be/Sg4U4r_AgJU?t=50s problem] on which this task is based.
 
;Problem:
Line 21:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">L(ln) File(‘data.txt’).read_lines()
I Float(ln.split(‘ ’, group_delimiters' 1B)[2]) > 6
print(ln)</langsyntaxhighlight>
 
{{out}}
Line 30:
5/18/1980 MountStHelens 7.6
</pre>
=={{header|68000 Assembly}}==
 
I'm going to make the assumption that all magnitudes are given as floating points rounded to one decimal place, e.g. "6.0" when the magnitude is exactly 6. That way I don't need to actually use floating point logic. The hardware print routines were omitted to keep things short, since the Sega Genesis doesn't have a built-in kernel and so I'd have to manually write to the video chip. Chances are you're not interested in seeing all that fluff when you'd rather look at the actual algorithm for the task.
 
<syntaxhighlight lang="68000devpac">;Macros
macro pushRegs 1
MOVEM.L \1,-(SP)
endm
;---------------------------------------------------------------------------
macro popRegs 1
MOVEM.L (SP)+,\1
endm
;---------------------------------------------------------------------------
 
;Ram Variables
ramArea equ $00FF0000
Cursor_X equ ramArea ;Ram for Cursor Xpos
Cursor_Y equ ramArea+1 ;Ram for Cursor Ypos
 
 
;cartridge header and init routine go here, I'll leave them out to keep things short. Execution falls through into here
;after the screen has been activated and the bitmap font loaded into VRAM
 
 
LEA earthquake,A3
move.l #3-1,d7 ;total line count in the file.
mainloop:
MOVE.B #'.',D0 ;find the period in each line.
MOVE.B #10,D1
jsr CharInThisLine
CMP.B #255,D0
BNE foundIt
jsr lineseek
jmp mainloop
foundIt:
;print only if magnitude is 6.1 or greater
suba.l #2,a4
cmp.b #'6',(a4)
bcs .skip
adda.l #2,a4
cmp.b #'0',(a4)
beq .skip
jsr printline
.skip:
DBRA D7,mainloop
jmp * ;end program
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; unixdict functions:
PrintLine:
MOVE.B (A3)+,D0
CMP.B #10,D0
BEQ .done
CMP.B #255,D0
BEQ .done
jsr PrintChar
bra PrintLine
.done:
ADDA.L #1,A0 ;inc past the line feed to the next word.
;fallthrough is intentional here
NewLine:
addq.b #1,(Cursor_Y) ;INC Y
clr.b (Cursor_X) ;Zero X
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
IgnoreCase:
;forces lower case.
;input: d0.b
;output: d0.b
CMP.B #'A',D0 ;compare to ascii code for A
BCS .done ;if less than A, keep looping.
CMP.B #'Z'+1,D0 ;compare to ascii code for Z
BCC .done ;if greater than Z, keep looping
OR.B #%00100000,D0 ;this "magic constant" turns upper case to lower case, since they're always 32 apart.
.done
RTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CharInThisLine:
pushregs d2-d4/a3
;input: d0 = char to search for. d1 = terminator of choice
MOVEQ #0,D3
.again:
MOVE.B (A3)+,D2
CMP.B D1,D2
BEQ .didntFindIt
pushlong d0
move.b d2,d0
jsr ignoreCase
move.b d0,d2
poplong d0
CMP.B D0,D2
BEQ .foundIt
addq.l #1,d3
bra .again
.didntFindIt:
MOVE.B #255,D0 ;RETURN 255 ON FAILURE
bra .exit
.foundIt:
MOVE.L D3,D0 ;RETURN ZERO-INDEXED POSITION OF CHAR ON SUCCESS
bra .exit
.exit:
move.l a3,a4
popregs d2-d4/a3
rts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
lineseek:
;advances pointer to the "next" word without printing anything.
MOVE.B (A3)+,D0
CMP.B #10,D0
BEQ .done
CMP.B #255,D0
BEQ .done
bra lineseek
.done:
RTS</syntaxhighlight>
{{out}}
<pre>8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6</pre>
 
=={{header|8080 Assembly}}==
Line 41 ⟶ 164:
automatically.
 
<langsyntaxhighlight lang="8080asm">FCB1: equ 5Ch ; FCB for first command line argument
puts: equ 9 ; CP/M syscall to print a string
fopen: equ 15 ; CP/M syscall to open a file
Line 107 ⟶ 230:
jmp 5
emsg: db 'Error!$'
line: equ $ ; Line buffer after program </langsyntaxhighlight>
 
{{out}}
Line 120 ⟶ 243:
 
A>quakes data.txt
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1
</pre>
 
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/quakes.txt quakes.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"
 
BYTE FUNC FindFirstNonspace(CHAR ARRAY s BYTE start)
WHILE start<=s(0) AND s(start)=32
DO
start==+1
OD
RETURN (start)
 
BYTE FUNC FindFirstSpace(CHAR ARRAY s BYTE start)
WHILE start<=s(0) AND s(start)#32
DO
start==+1
OD
RETURN (start)
 
BYTE FUNC Found(CHAR ARRAY s REAL POINTER value)
BYTE pos,v
CHAR ARRAY sub
 
pos=FindFirstNonspace(s,1)
pos=FindFirstSpace(s,pos)
pos=FindFirstNonspace(s,pos)
pos=FindFirstSpace(s,pos)
pos=FindFirstNonspace(s,pos)
IF pos>s(0) THEN RETURN (0) FI
 
SCopyS(sub,s,pos,s(0))
ValR(sub,v)
IF RealGreaterOrEqual(value,v)=0 THEN
RETURN (1)
FI
RETURN (0)
 
PROC Process(CHAR ARRAY fname REAL POINTER value BYTE search)
CHAR ARRAY line(255)
BYTE dev=[1]
 
Close(dev)
Open(dev,fname,4)
WHILE Eof(dev)=0
DO
InputSD(dev,line)
IF search=0 OR Found(line,value)=1 THEN
PrintE(line)
FI
OD
Close(dev)
RETURN
 
PROC Main()
CHAR ARRAY fname="H6:QUAKES.TXT"
REAL value
 
Put(125) PutE() ;clear the screen
IntToReal(6,value)
PrintF("Reading ""%S""...%E%E",fname)
Process(fname,value,0)
PutE()
Print("Searching for earthquakes > ")
PrintRE(value) PutE()
Process(fname,value,1)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Kernighans_large_earthquake_problem.png Screenshot from Atari 8-bit computer]
<pre>
Reading "H6:QUAKES.TXT"...
 
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
3/13/2009 CostaRica 5.1
1/23/4567 EdgeCase1 6
 
 
 
1/24/4567 EdgeCase2 6.0
1/25/4567 EdgeCase3 6.1
 
 
Searching for earthquakes > 6
 
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
Line 126 ⟶ 339:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">-- Kernighans large earthquake problem
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
Line 158 ⟶ 371:
end loop;
Close (Inpt_File);
end Main;</langsyntaxhighlight>
The file data.txt contains a 0 length line as well as a line composed of only blanks.
<pre>
Line 176 ⟶ 389:
1/25/4567 EdgeCase3 6.1
</pre>
 
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">IF FILE input file;
STRING file name = "data.txt";
open( input file, file name, stand in channel ) /= 0
Line 249 ⟶ 461:
# close the file #
close( input file )
FI</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
/* Kernighans large earthquake problem. */
 
#include <flow.h>
#include <flow-flow.h>
 
#define MAX_LINE 1000
 
DEF-MAIN(argv,argc)
MSET(fd, Event )
TOK-INIT
OPEN-INPUT("datos.txt")(fd)
COND( IS-NOT-FILE-ERROR? )
TOK-SEP( " " ), TOK(3)
WHILE( NOT( EOF(fd) ) )
LET( Event := USING(MAX_LINE) READ-LINE(fd) APPLY-TRM )
WHEN( LEN(Event) ){
WHEN( GT?( VAL(TOK-GET(Event)), 6 ) ){
PRNL( Event )
}
}
WEND
CLOSE(fd)
ELS
PRNL("Error: ", ~GET-STR-FILE-ERROR )
CEND
END
</syntaxhighlight>
{{out}}
<pre>
Data file: datos.txt.
 
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
3/13/2009 CostaRica 5.1
1/23/4567 EdgeCase1 6
 
1/24/4567 EdgeCase2 6.0
1/25/4567 EdgeCase3 6.1
</pre>
Output:
<pre>
$ hopper fl/evento.flw
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1
$
</pre>
<p>PROBLEM: What if the file contains more information than expected? We would have to validate the lines that can be processed. For example, we might have a file with the following information:
</p>
<pre>
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
3/13/2009 CostaRica 5.1
Total events: 3
 
1/23/4567 EdgeCase1 6
 
1/24/4567 EdgeCase2 6.0
1/25/4567 EdgeCase3 6.1
1/25/4567 EdgeCase4 6.1
Total events: 4
 
Total Events in periods: 7
</pre>
<p>There are lines with 3 tokens, and the third token is numeric: it would be shown as a valid record!</p>
<p>On the other hand, HOPPER processes dates in DD/MM/YYYY format, and the file records dates in MM/DD/YYYY format: therefore, it is necessary to exchange "DD" for "MM", because HOPPER does not allow other types of format for "dates".</p>
<p>The final program would be as follows:</p>
<syntaxhighlight lang="amazing hopper">
/* Kernighans large earthquake problem. */
 
#include <flow.h>
#include <flow-flow.h>
 
#define MAX_LINE 1000
#define SwapDayByMonth(_N_,_M_) TOK-SEP("/"), TOK(_N_), TOK-SWAP(_M_,Event), TOK-SEP(" ")
 
DEF-MAIN(argv,argc)
MSET(fd, Event )
TOK-INIT
OPEN-INPUT("datos.txt")(fd)
COND( IS-NOT-FILE-ERROR? )
TOK-SEP( " " )
WHILE( NOT( EOF(fd) ) )
LET( Event := USING(MAX_LINE) READ-LINE(fd) APPLY-TRM )
WHEN( LEN(Event) ){
WHEN( EQ?(TOK-COUNT( Event ),3) ){
Swap Day By Month(1,2)
WHEN( IS-DATE-VALID?( TOK(1) TOK-GET(Event) )){
TOK(3)
WHEN( GT?( VAL(TOK-GET(Event)), 6 ) ){
PRNL( Event )
}
}
}
}
WEND
CLOSE(fd)
ELS
PRNL("Error: ", ~GET-STR-FILE-ERROR )
CEND
END
</syntaxhighlight>
<p>And this is fast!</p>
<p>If you want to print the original dates, just add the line "Swap Day By Month(1,2)" before printing the result. In this case, the results are printed with the dates changed.</p>
{{out}}
<pre>
27/8/1883 Krakatoa 8.8
18/5/1980 MountStHelens 7.6
25/1/4567 EdgeCase3 6.1
25/1/4567 EdgeCase4 6.1
</pre>
 
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on kernighansEarthquakes(magnitudeToBeat)
-- A local "owner" for the long AppleScript lists. Speeds up references to their items and properties.
script o
Line 288 ⟶ 616:
end kernighansEarthquakes
 
kernighansEarthquakes(6)</langsyntaxhighlight>
 
===Functional===
Line 295 ⟶ 623:
while emphasising code reuse, and speed of writing and refactoring:
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
Line 434 ⟶ 762:
set my text item delimiters to dlm
str
end unlines</langsyntaxhighlight>
{{Out}}
<pre>Magnitudes above 6.0 in ~/Desktop/data.txt:
Line 447 ⟶ 775:
and the magnitude as an optional left argument (defaulting to 6).
 
<langsyntaxhighlight APLlang="apl">quakes←{
⍺←6
nl←⎕UCS 13 10
Line 454 ⟶ 782:
keep←⍺{0::0 ⋄ ⍺ < ⍎3⊃(~⍵∊4↑⎕TC)⊆⍵}¨lines
↑keep/lines
}</langsyntaxhighlight>
 
{{out}}
Line 471 ⟶ 799:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">data: {
3/13/2009 CostaRica 5.1
8/27/1883 Krakatoa 8.8
Line 480 ⟶ 808:
 
print first sort.descending.by:'magnitude map split.lines data =>
[to :earthquake split.words &]</langsyntaxhighlight>
 
{{out}}
Line 487 ⟶ 815:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk"> awk '$3 > 6' data.txt</langsyntaxhighlight>
 
=={{header|Bash}}==
<langsyntaxhighlight lang="bash">#!/bin/bash
while read line
do
[[ ${line##* } =~ ^([7-9]|6\.0*[1-9]).*$ ]] && echo "$line"
done < data.txt</langsyntaxhighlight>
 
{{out}}
Line 512 ⟶ 840:
 
=={{header|BASIC256}}==
<syntaxhighlight lang="basic256">
<lang BASIC256>
f = freefile
filename$ = "data.txt"
Line 524 ⟶ 852:
close f
end
</syntaxhighlight>
</lang>
 
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Line 555 ⟶ 883:
if (line) free(line);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 567 ⟶ 895:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.IO;
using System.Linq;
Line 585 ⟶ 913:
select parts;
 
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">// Randizo was here!
#include <iostream>
#include <fstream>
Line 631 ⟶ 959:
 
return 0;
}</langsyntaxhighlight>
 
New version:
<langsyntaxhighlight lang="cpp">// Jolkdarr was also here!
#include <iostream>
#include <iomanip>
Line 656 ⟶ 984:
cout << endl << "Number of quakes greater than 6 is " << count_quake << endl;
return 0;
}</langsyntaxhighlight>
 
=={{header|Cixl}}==
<langsyntaxhighlight lang="cixl">
use: cx;
 
Line 667 ⟶ 995:
$m1 6 >= $m2 0 > and {[$time @@s $place @@s $mag] say} if
} for
</syntaxhighlight>
</lang>
 
{{output}}
Line 679 ⟶ 1,007:
First, with a data file. This adds a fair amount of verbosity to COBOL. For something this one-off, a simpler cut using ACCEPT from standard in is shown.
 
<syntaxhighlight lang="cobolfree">*>
<lang cobol>
*> Kernighan large earthquake problem
*>
*> Tectonics: cobc -xj kernighan-earth-quakes.cob
*> Kernighan large earthquake problem
*> *> Tectonics: cobc -xj kernighan-earth-quakes.cobtxt with the 3 sample lines
*> ./kernighan-earth-quakes.txt with the 3 sample lines
*>
*> ./kernighan-earth-quakes
>>SOURCE FORMAT IS FREE
*>
IDENTIFICATION DIVISION.
>>SOURCE FORMAT IS FREE
PROGRAM-ID. quakes.
identification division.
program-id. quakes.
 
ENVIRONMENT DIVISION.
environment division.
CONFIGURATION SECTION.
configuration section.
REPOSITORY.
repository.
FUNCTION ALL INTRINSIC.
function all intrinsic.
 
INPUT-OUTPUT SECTION.
input-output section.
FILE-CONTROL.
file-control.
selectSELECT quake-data
ASSIGN assign toTO command-filename
ORGANIZATION IS organizationLINE is line sequentialSEQUENTIAL
STATUS status isIS quake-fd-status.
.
 
DATA DIVISION.
data division.
FILE SECTION.
file section.
fd FD quake-data recordRECORD varyingVARYING dependingDEPENDING onON line-length.
01 data-line pic x PICTURE IS X(32768).
 
WORKING-STORAGE SECTION.
working-storage section.
01 quake-fd-status 01 quake-fd-statusPICTURE picIS xxXX.
88 ok 88 ok values VALUES ARE "00", "01", "02", "03", "04",
"05", "06", "07", "08", "09".
88 no-more 88 no-more value VALUE IS "10".
88 io-error 88 io-error value high VALUE IS HIGH-valueVALUE.
 
01 line-length USAGE IS BINARY-LONG.
01 line-length usage binary-long.
01 date-time PICTURE IS X(10).
01 quake PICTURE IS X(20).
01 magnitude PICTURE IS 99V99.
 
01 command-filename 01 date-timePICTURE picIS xX(1080).
01 quake pic x(20).
01 magnitude pic 99v99.
 
PROCEDURE DIVISION.
01 command-filename pic x(80).
show-big-ones.
procedure division.
show-big-ones.
 
acceptACCEPT command-filename fromFROM commandCOMMAND-lineLINE
ifIF command-filename equalIS EQUAL TO spacesSPACES thenTHEN
moveMOVE "data.txt" toTO command-filename
endEND-ifIF
 
OPEN open inputINPUT quake-data
performPERFORM status-check
ifIF io-error thenTHEN
DISPLAY display trimTRIM(command-filename) " not found" uponUPON syserrSYSERR
gobackGOBACK
endEND-ifIF
 
readREAD quake-data
performPERFORM status-check
PERFORM perform untilUNTIL no-more orOR io-error
unstringUNSTRING data-line delimitedDELIMITED byBY allALL spacesSPACES
intoINTO date-time quake magnitude
endEND-unstringUNSTRING
 
IF magnitude if magnitudeIS greaterGREATER thanTHAN 6
displayDISPLAY date-time spaceSPACE quake spaceSPACE magnitude
endEND-ifIF
 
readREAD quake-data
performPERFORM status-check
endEND-performPERFORM
 
closeCLOSE quake-data
performPERFORM status-check
gobackGOBACK.
*> *> ****
 
status-check.
IF if notNOT ok andAND notNOT no-more thenTHEN *> not normal status, bailing
displayDISPLAY "io error: " quake-fd-status uponUPON syserrSYSERR
setSET io-error toTO trueTRUE
endEND-ifIF
EXIT PARAGRAPH.
 
END end programPROGRAM quakes.</langsyntaxhighlight>
 
{{output}}
Line 780 ⟶ 1,106:
A slighter shorter-version.
 
<langsyntaxhighlight lang="cobol"> *>
*> Tectonics: ./kerighan-earth-quakes <quakes.txt
identificationIDENTIFICATION divisionDIVISION.
programPROGRAM-idID. quakes.
 
dataDATA divisionDIVISION.
 
workingWORKING-storageSTORAGE sectionSECTION.
01 data-line pic x PICTURE IS X(32768).
88 no-more value high VALUE IS HIGH-valuesVALUES.
 
01 date-time pic x PICTURE IS X(10).
01 quake pic x PICTURE IS X(20).
01 magnitude pic 99v99 PICTURE IS 99V99.
 
procedurePROCEDURE divisionDIVISION.
show-big-ones.
 
acceptACCEPT data-line onON exceptionEXCEPTION setSET no-more toTO trueTRUE endEND-acceptACCEPT
performPERFORM untilUNTIL no-more
unstringUNSTRING data-line delimitedDELIMITED byBY allALL spacesSPACES
intoINTO date-time quake magnitude
endEND-unstringUNSTRING
 
ifIF magnitude greaterIS thanGREATER THAN 6
displayDISPLAY date-time spaceSPACE quake spaceSPACE magnitude
endEND-ifIF
 
acceptACCEPT data-line onON exceptionEXCEPTION setSET no-more toTO trueTRUE endEND-acceptACCEPT
endEND-performPERFORM
 
gobackGOBACK.
endEND programPROGRAM quakes.</langsyntaxhighlight>
 
That cut would be used as <pre>prompt$ ./kernighans-large-earthquakes <quakes.txt</pre>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
include "file.coh";
 
Line 898 ⟶ 1,224:
end if;
 
ForEachLine(&quakes, PrintIfGt6); </langsyntaxhighlight>
 
{{out}}
Line 912 ⟶ 1,238:
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Controls,SysUtils,Classes,StdCtrls,ExtCtrls}}
This code takes advantage of the standard Delphi "TStringGrid" object to do most of the heavy lifting. It is initially used to read the earthquake file into memory, breaking it up into of individual lines as it goes. Then the individual fields are stored in structurs/records attached to the data. finally, the data is sorted by magnitude so the earthquakes of magnitudes greater than six can be extracted. Because the data is now neatly organized in memory, all kinds of other process could be done, including sorting it by date or location. To make the problem more realistic, I extracted actual earthquake data from the first few months of 2023. I've post the data for other people to test here: [https://fountainware.com/download/EarthQuakes.txt EarthQuakes.txt]
 
<syntaxhighlight lang="Delphi">
{Structure used to contain all the earthquake data}
 
type TQuakeInfo = record
Date: TDate;
Name: string;
Mag: double;
end;
type PQuakeInfo = ^TQuakeInfo;
 
{Used to contain individual fields of the earthquake data}
 
type TStringArray = array of string;
 
 
function SortCompare(List: TStringList; Index1, Index2: Integer): Integer;
{Custom sort routine to sort data by magnitude }
var QI1,QI2: TQuakeInfo;
begin
QI1:=PQuakeInfo(List.Objects[Index1])^;
QI2:=PQuakeInfo(List.Objects[Index2])^;
Result:=Round(QI2.Mag*10)-Round(QI1.Mag*10);
end;
 
procedure GetFields(S: string; var SA: TStringArray);
{Extract the three fields from each row of data}
var I,F: integer;
begin
SetLength(SA,3);
for I:=0 to High(SA) do SA[I]:='';
F:=0;
for I:=1 to Length(S) do
if S[I] in [#$09,#$20] then Inc(F)
else SA[F]:=SA[F]+S[I];
end;
 
procedure AnalyzeEarthQuakes(Filename: string; Memo: TMemo);
{Read earhtquake data from specified file}
{Extract the individual fields and sort and display it}
var SL: TStringList;
var I: integer;
var S: string;
var FA: TStringArray;
var QI: PQuakeInfo;
begin
SL:=TStringList.Create;
try
{Read file, separating it into rows}
SL.LoadFromFile(Filename);
{Process each row}
for I:=0 to SL.Count-1 do
begin
S:=SL[I];
{Separate row into fields}
GetFields(S,FA);
{Store data as objects in TStringList}
New(QI);
QI.Date:=StrToDate(FA[0]);
QI.Name:=FA[1];
QI.Mag:=StrToFloat(FA[2]);
SL.Objects[I]:=TObject(QI);
end;
{Sort data by magnitude}
SL.CustomSort(SortCompare);
{Display sorted data}
for I:=0 to SL.Count-1 do
begin
if PQuakeInfo(SL.Objects[I]).Mag<6 then break;
S:=FormatDateTime('dd/mm/yyyy', PQuakeInfo(SL.Objects[I]).Date);
S:=S+Format(' %-34s',[PQuakeInfo(SL.Objects[I]).Name]);
S:=S+Format(' %5f',[PQuakeInfo(SL.Objects[I]).Mag]);
Memo.Lines.Add(S);
end;
{Dispose of memory}
finally
for I:=0 to SL.Count-1 do Dispose(PQuakeInfo(SL.Objects[I]));
SL.Free;
end;
end;
 
 
procedure ShowEarthQuakes(Memo: TMemo);
begin
AnalyzeEarthQuakes('EarthQuakes.txt',Memo);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
06/02/2023 Turkey_Kahramanmaras 7.80
09/01/2023 Indonesia_Maluku 7.60
06/02/2023 Turkey_Kahramanmaras 7.50
02/04/2023 Papua_New_Guinea_East_Sepik 7.00
16/03/2023 New_Zealand_Kermadec_Islands 7.00
18/01/2023 Indonesia_North_Maluku 7.00
14/04/2023 Indonesia_East_Java 7.00
08/01/2023 Vanuatu_Sanma 7.00
04/03/2023 New_Zealand_Kermadec_Islands 6.90
23/02/2023 Tajikistan_Gorno-Badakhshan 6.90
18/03/2023 Ecuador_Guayas 6.80
20/01/2023 Argentina_Santiago_del_Estero 6.80
18/04/2023 South_of_theFiji_Islands 6.70
06/02/2023 Turkey_Gaziantep 6.70
01/03/2023 Papua_New_Guinea_West_New_Britain 6.60
02/03/2023 Vanuatu_Sanma 6.50
03/04/2023 Russia_Kamchatka_Krai 6.50
22/03/2023 Argentina_Jujuy 6.50
21/03/2023 Afghanistan_Badakhshan 6.50
24/01/2023 Argentina_Santiago_del_Estero 6.40
19/04/2023 Papua_New_Guinea_West_New_Britain 6.30
16/01/2023 Japan_Bonin_Islands 6.30
20/02/2023 Turkey_Hatay 6.30
23/02/2023 Indonesia_North_Maluku 6.30
21/04/2023 Indonesia_Southeast_Sulawesi 6.30
14/03/2023 Papua_New_Guinea_Madang 6.30
30/03/2023 Chile_Maule 6.30
04/04/2023 Panama_Chiriqu- 6.30
25/02/2023 Papua_New_Guinea_West_New_Britain 6.20
04/04/2023 Philippines_Bicol 6.20
27/03/2023 Solomon_Islands_Isabel 6.10
03/04/2023 Indonesia_North_Sumatra 6.10
17/02/2023 Indonesia_Maluku 6.10
15/02/2023 Philippines_Bicol 6.10
13/02/2023 New_Zealand_Kermadec_Islands 6.10
20/01/2023 France_Guadeloupe 6.10
15/01/2023 Indonesia_Aceh 6.10
28/03/2023 Japan_Hokkaido 6.00
18/01/2023 Indonesia_Gorontalo 6.00
01/02/2023 Philippines_Davao 6.00
05/01/2023 Afghanistan_Badakhshan 6.00
26/01/2023 New_Zealand_Kermadec_Islands 6.00
06/02/2023 Turkey_Kahramanmaras 6.00
06/02/2023 Turkey_Malatya 6.00
25/02/2023 Japan_Hokkaido 6.00
13/04/2023 Canada_British_Columbia 6.00
</pre>
 
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.conv : to;
import std.regex : ctRegex, split;
import std.stdio : File, writeln;
Line 929 ⟶ 1,399:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Those earthquakes with a magnitude > 6.0 are:
Line 936 ⟶ 1,406:
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(with-temp-buffer
<lang lisp>#!/usr/bin/env emacs --script
(insert-file-contents "data.txt")
 
(goto-char (point-min))
(dolist (arg command-line-args-left)
(find-file arg)
(while (not (eobp))
(let* ((line (buffer-substring (line-beginning-position)
Line 946 ⟶ 1,415:
(when (> (string-to-number magn) 6.0)
(message line)))
(forward-line 1))))</langsyntaxhighlight>
 
=={{header|Factor}}==
<code>lines</code> is a convenience word that reads lines from standard input. If you don't want to type them all in yourself, it is suggested that you give the program a file to read. For example, on the Windows command line: <code>factor kernighan.factor < earthquakes.txt</code>
<langsyntaxhighlight lang="factor">USING: io math math.parser prettyprint sequences splitting ;
IN: rosetta-code.kernighan
 
lines [ "\s" split last string>number 6 > ] filter .</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim As Long f
f = Freefile
Line 974 ⟶ 1,443:
Close #f
Sleep
</syntaxhighlight>
</lang>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,009 ⟶ 1,478:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,021 ⟶ 1,490:
=={{header|Groovy}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="groovy">import java.util.regex.Pattern
 
class LargeEarthquake {
Line 1,034 ⟶ 1,503:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Those earthquakes with a magnitude > 6.0 are:
Line 1,042 ⟶ 1,511:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import qualified Data.ByteString.Lazy.Char8 as C
 
main :: IO ()
Line 1,051 ⟶ 1,520:
(\x ->
[ x
| 6 < (read (last (C.unpack <$> C.words x)) :: Float) ])</langsyntaxhighlight>
{{Out}}
<pre>"8/27/1883 Krakatoa 8.8"
Line 1,057 ⟶ 1,526:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
NB. this program is designed for systems where the line ending is either LF or CRLF
 
Line 1,072 ⟶ 1,541:
(y <: magnitudes) # lines
)
</syntaxhighlight>
</lang>
 
<pre>
Line 1,082 ⟶ 1,551:
=={{header|Java}}==
Input file contains sample data shown in the task
<syntaxhighlight lang="java">
<lang Java>
import java.io.BufferedReader;
import java.io.FileReader;
Line 1,103 ⟶ 1,572:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,113 ⟶ 1,582:
=={{header|JavaScript}}==
Input file contains sample data shown in the task. The code below uses Nodejs to read the file.
<syntaxhighlight lang="javascript">
<lang JavaScript>
const fs = require("fs");
const readline = require("readline");
Line 1,136 ⟶ 1,605:
}
});
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,142 ⟶ 1,611:
8/27/1883 Krakatoa 8.8
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]].
 
<syntaxhighlight 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 )
</syntaxhighlight>
{{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>
 
=={{header|Julia}}==
Using the example data as a small text file.
<langsyntaxhighlight lang="julia">using DataFrames, CSV
 
df = CSV.File("kernighansproblem.txt", delim=" ", ignorerepeated=true,
Line 1,153 ⟶ 1,659:
 
println(filter(row -> row[:Magnitude] > 6, df))
</langsyntaxhighlight> {{output}} <pre>
2×3 DataFrame
│ Row │ Date │ Location │ Magnitude │
Line 1,163 ⟶ 1,669:
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">arg pop nip len dup
 
( [get nip]
Line 1,180 ⟶ 1,686:
$f fclose
 
"End " input</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.40
 
import java.io.File
Line 1,193 ⟶ 1,699:
if (it.split(r)[2].toDouble() > 6.0) println(it)
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,206 ⟶ 1,712:
=={{header|Lua}}==
For each line, the Lua pattern "%S+$" is used to capture between the final space character and the end of the line.
<langsyntaxhighlight lang="lua">-- arg[1] is the first argument provided at the command line
for line in io.lines(arg[1] or "data.txt") do -- use data.txt if arg[1] is nil
magnitude = line:match("%S+$")
if tonumber(magnitude) > 6 then print(line) end
end</langsyntaxhighlight>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Find_Magnitude {
data$={8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
3/13/2009 CostaRica 5.1
1/23/4567 EdgeCase1 6
1/24/4567 EdgeCase2 6.0
1/25/4567 EdgeCase3 6.1
}
Open "data.txt" for output as F
Print #F, data$;
Close #F
Open "data.txt" for input as F
While not eof(#F)
Line Input #f, part$
REM if val(mid$(part$,30))>6 then print part$
if val(mid$(part$,rinstr(rtrim$(part$)," ")))>6 then print part$
End While
Close #F
}
Find_Magnitude
 
 
</syntaxhighlight>
 
 
{{out}}
<pre>
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Import["data.txt", "Table"] // Select[Last /* GreaterThan[6]]</langsyntaxhighlight>
 
=={{header|Nim}}==
Here is one way to do that:
 
<langsyntaxhighlight Nimlang="nim">import strscans
 
for line in "data.txt".lines:
Line 1,226 ⟶ 1,765:
if magnitude > 6:
echo line
# else wrong line: ignore.</langsyntaxhighlight>
 
Here is another way with less checks:
 
<langsyntaxhighlight Nimlang="nim">import strutils
 
for line in "data.txt".lines:
let magnitude = line.rsplit(' ', 1)[1]
if magnitude.parseFloat() > 6:
echo line</langsyntaxhighlight>
 
{{out}}
Line 1,242 ⟶ 1,781:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">perl -n -e '/(\S+)\s*$/ and $1 > 6 and print' data.txt</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence cl = command_line()
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
string filename = iff(length(cl)>=3?cl[3]:"e02.txt")
<span style="color: #008080;">constant</span> <span style="color: #000000;">filename</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"data.txt"</span>
integer fn = open(filename,"r")
<span style="color: #004080;">string</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">or</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">file_exists</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">"""
if fn=-1 then crash("cannot open filename") end if
8/27/1883 Krakatoa 8.8
while 1 do
5/18/1980 MountStHelens 7.6
object line = gets(fn)
3/13/2009 CostaRica 5.1"""</span> <span style="color: #0000FF;">:</span> <span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">))</span>
if line=-1 then exit end if
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split_any</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\r\n"</span><span style="color: #0000FF;">)</span>
line = substitute(trim(line),"\t"," ")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence r = scanf(line,"%s %f")
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%s %f"</span><span style="color: #0000FF;">)</span>
if length(r)=1 and r[1][2]>6 then ?line end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]></span><span style="color: #000000;">6</span> <span style="color: #008080;">then</span>
end while
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
close(fn)</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,264 ⟶ 1,806:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">argument tail nip len dup
if
get nip
Line 1,285 ⟶ 1,827:
endif
endwhile
fclose</langsyntaxhighlight>
 
=={{header|PHP}}==
Parse using PHP's fscanf().
<langsyntaxhighlight lang="php"><?php
 
// make sure filename was specified on command line
Line 1,306 ⟶ 1,848:
 
fclose( $fh );
</syntaxhighlight>
</lang>
 
Usage: Specify file name on command line. Ex:
Line 1,318 ⟶ 1,860:
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
<lang PicoLisp>
(load "@lib/misc.l")
 
Line 1,327 ⟶ 1,869:
(prinl (align -10 Date) " " (align -15 Quake) " " Mag)))))
(bye)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,337 ⟶ 1,879:
{{works with|SWI Prolog}}
Example command line: <code>swipl kernighans_earthquake.pl earthquake.txt</code>.
<langsyntaxhighlight lang="prolog">:- initialization(main, main).
 
process_line(Line):-
Line 1,365 ⟶ 1,907:
main(_):-
swritef(Message, 'File argument is missing\n', []),
write(user_error, Message).</langsyntaxhighlight>
 
{{out}}
Line 1,374 ⟶ 1,916:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole() And ReadFile(0,"data.txt")
PrintN("Those earthquakes with a magnitude > 6.0 are:")
While Not Eof(0)
Line 1,384 ⟶ 1,926:
CloseFile(0)
Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Those earthquakes with a magnitude > 6.0 are:
Line 1,392 ⟶ 1,934:
=={{header|Python}}==
Typed into a bash shell or similar:
<langsyntaxhighlight lang="python">python -c '
with open("data.txt") as f:
for ln in f:
if float(ln.strip().split()[2]) > 6:
print(ln.strip())'</langsyntaxhighlight>
 
 
Or, if scale permits a file slurp and a parse retained for further processing, we can combine the parse and filter with a concatMap abstraction:
 
<langsyntaxhighlight lang="python">from os.path import expanduser
from functools import (reduce)
from itertools import (chain)
Line 1,438 ⟶ 1,980:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>[('8/27/1883', 'Krakatoa', '8.8'), ('5/18/1980', 'MountStHelens', '7.6')]</pre>
Line 1,448 ⟶ 1,990:
This is just a file filter, matching lines are printed out.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(with-input-from-file "data/large-earthquake.txt"
Line 1,454 ⟶ 1,996:
(for ((s (in-port read-line))
#:when (> (string->number (third (string-split s))) 6))
(displayln s))))</langsyntaxhighlight>
 
 
Or, defining a list -> list function in terms of '''filter''':
<langsyntaxhighlight lang="scheme">#lang racket
 
; largeQuakes :: Int -> [String] -> [String]
Line 1,489 ⟶ 2,031:
; unlines :: [String] -> String
(define (unlines xs)
(string-join xs "\n"))</langsyntaxhighlight>
 
{{out}}
Line 1,496 ⟶ 2,038:
 
To combine filtering with more pre-processing, we can use '''concatMap''' in place of '''filter''':
<langsyntaxhighlight lang="scheme">#lang racket
 
(require gregor) ; Date parsing
Line 1,535 ⟶ 2,077:
(define (readFile fp)
(file->string
(expand-user-path fp)))</langsyntaxhighlight>
{{Out}}
<pre>(#<date 1883-08-27> "Krakatoa" 8.8)
Line 1,544 ⟶ 2,086:
{{works with|Rakudo|2018.03}}
Pass in a file name, or use default for demonstration purposes.
<syntaxhighlight lang="raku" perl6line>$_ = @*ARGS[0] ?? @*ARGS[0].IO !! q:to/END/;
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
Line 1,550 ⟶ 2,092:
END
 
map { .say if .words[2] > 6 }, .lines;</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,559 ⟶ 2,101:
:::* &nbsp; the number of records that met the qualifying magnitude
:::* &nbsp; the qualifying magnitude
<langsyntaxhighlight lang="rexx">/*REXX program to read a file containing a list of earthquakes: date, site, magnitude.*/
parse arg iFID mMag . /*obtain optional arguments from the CL*/
if iFID=='' | iFID=="," then iFID= 'earthquakes.dat' /*Not specified? Then use default*/
Line 1,577 ⟶ 2,119:
say
if j==0 then say er 'file ' iFID " is empty or not found."
else say # ' earthquakes listed whose magnitude is ≥ ' mMag</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,593 ⟶ 2,135:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Kernighans large earthquake problem
 
Line 1,619 ⟶ 2,161:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,654 ⟶ 2,196:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::io::{BufRead, BufReader};
 
Line 1,672 ⟶ 2,214:
 
Ok(())
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">scala.io.Source.fromFile("data.txt").getLines
.map("\\s+".r.split(_))
.filter(_(2).toDouble > 6.0)
.map(_.mkString("\t"))
.foreach(println)</langsyntaxhighlight>
 
=={{header|Snobol}}==
Line 1,685 ⟶ 2,227:
This is hard-coded to read the input from "data.txt".
 
<langsyntaxhighlight lang="snobol"> input(.quake, 1,, 'data.txt') :f(err)
num = '.0123456789'
 
Line 1,693 ⟶ 2,235:
 
err output = 'Error!'
end</langsyntaxhighlight>
 
{{output}}
Line 1,713 ⟶ 2,255:
Expects the program to be started with the path to the data file.
 
<langsyntaxhighlight Swiftlang="swift">import Foundation
 
guard let path = Array(CommandLine.arguments.dropFirst()).first else {
Line 1,731 ⟶ 2,273:
 
print(line)
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
Inspired by awk.
<langsyntaxhighlight lang="tcl">catch {console show} ;## show console when running from tclwish
catch {wm withdraw .}
 
Line 1,751 ⟶ 2,293:
if {$f3 > 6} { puts "$line" }
}
close $fh </langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.IO
 
Module Module1
Line 1,772 ⟶ 2,314:
End Sub
 
End Module</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import os
fn main() {
lines := os.read_lines('data.txt')?
println('Those earthquakes with a magnitude > 6.0 are:\n')
for line in lines {
fields := line.fields()
mag := fields[2].f64()
if mag > 6.0 {
println(line)
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
Those earthquakes with a magnitude > 6.0 are:
 
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-pattern}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "os" for Process
import "./pattern" for Pattern
 
var args = Process.arguments
Line 1,791 ⟶ 2,355:
var mag = Num.fromString(data[2])
if (mag > 6) System.print(line)
}</langsyntaxhighlight>
 
{{out}}
Line 1,799 ⟶ 2,363:
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
</pre>
 
=={{header|XPL0}}==
Usage: quake <data.txt
<syntaxhighlight lang="xpl0">int C;
[loop [OpenO(8); \get line from input file
repeat C:= ChIn(1);
ChOut(8, C); \save it in buffer device 8
if C = $1A\EOF\ then quit;
until C = $0A\LF\;
OpenI(8);
repeat until ChIn(8) <= $20\space\;
repeat until ChIn(8) > $20\space\;
repeat until ChIn(8) <= $20\space\;
if RlIn(8) > 6.0 then
[OpenI(8); \output saved line to console
repeat C:= ChIn(8);
ChOut(0, C);
until C = $0A\LF\;
];
];
]</syntaxhighlight>
 
{{out}}
<pre>
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1
 
for data.txt
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
3/13/2009 CostaRica 5.1
1/23/4567 EdgeCase1 6
1/24/4567 EdgeCase2 6.0
1/25/4567 EdgeCase3 6.1
</pre>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">if peek("argument") then
filename$ = peek$("argument")
else
Line 1,816 ⟶ 2,416:
if val(tok$(3)) > 6 print a$
wend
close a</langsyntaxhighlight>
 
=={{header|zkl}}==
Line 1,822 ⟶ 2,422:
is bad practice so I don't do it (written so text is automatically
converted to float).
<langsyntaxhighlight lang="zkl">fcn equake(data,out=Console){
data.pump(out,fcn(line){ 6.0line.split()[-1] },Void.Filter)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">equake(Data(Void,
#<<<
"8/27/1883 Krakatoa 8.8\n"
Line 1,831 ⟶ 2,431:
"3/13/2009 CostaRica 5.1\n"
#<<<
));</langsyntaxhighlight>
or
<langsyntaxhighlight lang="zkl">equake(File("equake.txt"));</langsyntaxhighlight>
or
<langsyntaxhighlight lang="zkl">$ zkl --eval 'File.stdin.pump(Console,fcn(line){ 6.0<line.split()[-1] },Void.Filter)' < equake.txt</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits