Kernighans large earthquake problem: Difference between revisions

Content added Content deleted
(Add source for Rust)
Line 406: Line 406:
file >> s1 >> s2 >> rate;
file >> s1 >> s2 >> rate;
if (rate > 6.0) {
if (rate > 6.0) {
cout << s1 << setw(20) << s2 << " " << rate << endl;
cout << s1 << setw(20) << s2 << " " << rate << endl;
count_quake++;
count_quake++;
}
}
}
}
Line 779: Line 779:


[dup split 3 get tonum 6 > ( [drop print nl] [drop drop] ) if]
[dup split 3 get tonum 6 > ( [drop print nl] [drop drop] ) if]
[$f fgets dup -1 #]
[$f fgets dup -1 #]
while
while


Line 842: Line 842:
<lang Phixmonti>argument tail nip len dup
<lang Phixmonti>argument tail nip len dup
if
if
get nip
get nip
else
else
drop drop "data.txt"
drop drop "data.txt"
endif
endif


Line 853: Line 853:
true
true
while
while
dup fgets
dup fgets
dup 0 < if
dup 0 < if
drop false
drop false
else
else
dup split 3 get tonum 6 > if drop print else drop drop endif
dup split 3 get tonum 6 > if drop print else drop drop endif
true
true
endif
endif
endwhile
endwhile
fclose</lang>
fclose</lang>
Line 869: Line 869:
// make sure filename was specified on command line
// make sure filename was specified on command line
if ( ! isset( $argv[1] ) )
if ( ! isset( $argv[1] ) )
die( 'Data file name required' );
die( 'Data file name required' );


// open file and check for success
// open file and check for success
if ( ! $fh = fopen( $argv[1], 'r' ) )
if ( ! $fh = fopen( $argv[1], 'r' ) )
die ( 'Cannot open file: ' . $argv[1] );
die ( 'Cannot open file: ' . $argv[1] );


while ( list( $date, $loc, $mag ) = fscanf( $fh, "%s %s %f" ) ) {
while ( list( $date, $loc, $mag ) = fscanf( $fh, "%s %s %f" ) ) {
if ( $mag > 6 ) {
if ( $mag > 6 ) {
printf( "% -12s % -19s %.1f\n", $date, $loc, $mag );
printf( "% -12s % -19s %.1f\n", $date, $loc, $mag );
}
}
}
}


Line 900: Line 900:
(until (eof)
(until (eof)
(let (Date (read) Quake (read) Mag (read))
(let (Date (read) Quake (read) Mag (read))
(when (> Mag 6)
(when (> Mag 6)
(prinl (align -10 Date) " " (align -15 Quake) " " Mag)))))
(prinl (align -10 Date) " " (align -15 Quake) " " Mag)))))
(bye)
(bye)
</lang>
</lang>
Line 1,211: Line 1,211:
1849-09-09 Pym 9.0
1849-09-09 Pym 9.0
</pre>
</pre>

=={{header|Rust}}==
<lang Rust>fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::io::{BufRead, BufReader};

for line in BufReader::new(std::fs::OpenOptions::new().read(true).open("data.txt")?).lines() {
let line = line?;

let magnitude = line
.split_whitespace()
.nth(2)
.and_then(|it| it.parse::<f32>().ok())
.ok_or_else(|| format!("Could not parse scale: {}", line))?;

if magnitude > 6.0 {
println!("{}", line);
}
}

Ok(())
}</lang>


=={{header|Scala}}==
=={{header|Scala}}==
Line 1,245: Line 1,266:
=={{header|Tcl}}==
=={{header|Tcl}}==
Inspired by awk.
Inspired by awk.
<lang tcl>catch {console show} ;## show console when running from tclwish
<lang tcl>catch {console show} ;## show console when running from tclwish
catch {wm withdraw .}
catch {wm withdraw .}


set filename "data.txt"
set filename "data.txt"
set fh [open $filename]
set fh [open $filename]
set NR 0 ;# number-of-record, means linenumber
set NR 0 ;# number-of-record, means linenumber


while {[gets $fh line]>=0} { ;# gets returns length of line, -1 means eof
while {[gets $fh line]>=0} { ;# gets returns length of line, -1 means eof
incr NR
incr NR
set line2 [regexp -all -inline {\S+} $line] ;# reduce multiple whitespace
set line2 [regexp -all -inline {\S+} $line] ;# reduce multiple whitespace
set fld [split $line2] ;# split line into fields, at whitespace
set fld [split $line2] ;# split line into fields, at whitespace
set f3 [lindex $fld 2] ;# zero-based
set f3 [lindex $fld 2] ;# zero-based
#set NF [llength $fld] ;# number-of-fields
#set NF [llength $fld] ;# number-of-fields


if {$f3 > 6} { puts "$line" }
if {$f3 > 6} { puts "$line" }