Kernighans large earthquake problem: Difference between revisions

Add source for Rust
(Add source for Rust)
Line 406:
file >> s1 >> s2 >> rate;
if (rate > 6.0) {
cout << s1 << setw(20) << s2 << " " << rate << endl;
count_quake++;
}
}
Line 779:
 
[dup split 3 get tonum 6 > ( [drop print nl] [drop drop] ) if]
[$f fgets dup -1 #]
while
 
Line 842:
<lang Phixmonti>argument tail nip len dup
if
get nip
else
drop drop "data.txt"
endif
 
Line 853:
true
while
dup fgets
dup 0 < if
drop false
else
dup split 3 get tonum 6 > if drop print else drop drop endif
true
endif
endwhile
fclose</lang>
Line 869:
// make sure filename was specified on command line
if ( ! isset( $argv[1] ) )
die( 'Data file name required' );
 
// open file and check for success
if ( ! $fh = fopen( $argv[1], 'r' ) )
die ( 'Cannot open file: ' . $argv[1] );
 
while ( list( $date, $loc, $mag ) = fscanf( $fh, "%s %s %f" ) ) {
if ( $mag > 6 ) {
printf( "% -12s % -19s %.1f\n", $date, $loc, $mag );
}
}
}
 
Line 900:
(until (eof)
(let (Date (read) Quake (read) Mag (read))
(when (> Mag 6)
(prinl (align -10 Date) " " (align -15 Quake) " " Mag)))))
(bye)
</lang>
Line 1,211:
1849-09-09 Pym 9.0
</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}}==
Line 1,245 ⟶ 1,266:
=={{header|Tcl}}==
Inspired by awk.
<lang tcl>catch {console show} ;## show console when running from tclwish
catch {wm withdraw .}
 
set filename "data.txt"
set fh [open $filename]
set NR 0 ;# number-of-record, means linenumber
 
while {[gets $fh line]>=0} { ;# gets returns length of line, -1 means eof
incr NR
set line2 [regexp -all -inline {\S+} $line] ;# reduce multiple whitespace
set fld [split $line2] ;# split line into fields, at whitespace
set f3 [lindex $fld 2] ;# zero-based
#set NF [llength $fld] ;# number-of-fields
 
if {$f3 > 6} { puts "$line" }
Anonymous user