Text processing/2: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: removed OVERFLOW from PRE html tag. |
→{{header|Go}}: gofmt; validate date field; use bufio.Scanner to simplify and remove long line err; simplify map handling, in particular remove string([]byte(f[0])) as f[0] is already a string |
||
Line 829: | Line 829: | ||
import ( |
import ( |
||
"bufio" |
|||
"fmt" |
|||
"log" |
|||
"os" |
|||
"strconv" |
|||
"strings" |
|||
"time" |
|||
) |
) |
||
const ( |
|||
var fn = "readings.txt" |
|||
filename = "readings.txt" |
|||
readings = 24 // per line |
|||
fields = readings*2 + 1 // per line |
|||
dateFormat = "2006-01-02" |
|||
) |
|||
func main() { |
func main() { |
||
file, err := os.Open(filename) |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
return |
|||
defer file.Close() |
|||
} |
|||
var allGood, uniqueGood int |
|||
defer f.Close() |
|||
// map records not only dates seen, but also if an all-good record was |
|||
var allGood, uniqueGood int |
|||
// seen for the key date. |
|||
// map records not only dates seen, but also if an all-good record was |
|||
m := make(map[time.Time]bool) |
|||
// seen for the key date. |
|||
s := bufio.NewScanner(file) |
|||
m := make(map[string]bool) |
|||
for s.Scan() { |
|||
f := strings.Fields(s.Text()) |
|||
line, pref, err := lr.ReadLine() |
|||
if len(f) != fields { |
|||
log.Fatal("unexpected format,", len(f), "fields.") |
|||
break |
|||
} |
|||
} |
|||
ts, err := time.Parse(dateFormat, f[0]) |
|||
if err != nil { |
|||
if err != nil { |
|||
fmt.Println(err) |
|||
log.Fatal(err) |
|||
return |
|||
} |
|||
} |
|||
good := true |
|||
if pref { |
|||
for i := 1; i < fields; i += 2 { |
|||
fmt.Println("Unexpected long line.") |
|||
flag, err := strconv.Atoi(f[i+1]) |
|||
return |
|||
if err != nil { |
|||
} |
|||
log.Fatal(err) |
|||
f := strings.Fields(string(line)) |
|||
} |
|||
if len(f) != 49 { |
|||
if flag > 0 { // value is good |
|||
fmt.Println("unexpected format,", len(f), "fields.") |
|||
_, err := strconv.ParseFloat(f[i], 64) |
|||
return |
|||
if err != nil { |
|||
} |
|||
log.Fatal(err) |
|||
good := true |
|||
} |
|||
for i := 1; i < 49; i += 2 { |
|||
} else { // value is bad |
|||
flag, err := strconv.Atoi(f[i+1]) |
|||
good = false |
|||
if err != nil { |
|||
} |
|||
fmt.Println(err) |
|||
} |
|||
return |
|||
if good { |
|||
} |
|||
allGood++ |
|||
if flag > 0 { // value is good |
|||
} |
|||
_, err := strconv.ParseFloat(f[i], 64) |
|||
previouslyGood, seen := m[ts] |
|||
if err != nil { |
|||
if seen { |
|||
fmt.Println(err) |
|||
fmt.Println("Duplicate datestamp:", f[0]) |
|||
return |
|||
} |
|||
} |
|||
m[ts] = previouslyGood || good |
|||
} else { // value is bad |
|||
if !previouslyGood && good { |
|||
good = false |
|||
uniqueGood++ |
|||
} |
|||
} |
|||
} |
|||
} |
|||
if good { |
|||
if err := s.Err(); err != nil { |
|||
allGood++ |
|||
log.Fatal(err) |
|||
} |
|||
} |
|||
previouslyGood, seen := m[f[0]] |
|||
if seen { |
|||
fmt.Println("\nData format valid.") |
|||
fmt.Println(allGood, "records with good readings for all instruments.") |
|||
if !previouslyGood && good { |
|||
fmt.Println(uniqueGood, |
|||
m[string([]byte(f[0]))] = true |
|||
"unique dates with good readings for all instruments.") |
|||
uniqueGood++ |
|||
} |
|||
} else { |
|||
m[string([]byte(f[0]))] = good |
|||
if good { |
|||
uniqueGood++ |
|||
} |
|||
} |
|||
} |
|||
fmt.Println("\nData format valid.") |
|||
fmt.Println(allGood, "records with good readings for all instruments.") |
|||
fmt.Println(uniqueGood, |
|||
"unique dates with good readings for all instruments.") |
|||
}</lang> |
}</lang> |
||
{{out}} |
|||
Output: |
|||
<pre> |
<pre> |
||
Duplicate datestamp: 1990-03-25 |
Duplicate datestamp: 1990-03-25 |