Text processing/1: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 38:
{{trans|Python}}
<
V nodata_max = -1
[String] nodata_maxline
Line 78:
print(‘Readings = #6’.format(num_file))
print(‘Average = #6.3’.format(tot_file / num_file))
print("\nMaximum run(s) of #. consecutive false readings ends at line starting with date(s): #.".format(nodata_max, nodata_maxline.join(‘, ’)))</
{{out}}
Line 97:
=={{header|Ada}}==
{{libheader|Simple components for Ada}}
<
with Strings_Edit; use Strings_Edit;
with Strings_Edit.Floats; use Strings_Edit.Floats;
Line 172:
Close (File);
Put_Line ("Syntax error at " & Image (Current.Line) & ':' & Image (Max.Pointer));
end Data_Munging;</
The implementation performs minimal checks. The average is calculated over all valid data. For the maximal chain of consequent invalid data, the source line number, the column number, and the time stamp of the first invalid data is printed.
{{out|Sample output}}
Line 181:
=={{header|Aime}}==
<
file f;
list l;
Line 215:
o_form("Averaged /d3/ over ~ readings.\n", s / count, count);
o_("Longest bad run ", max_bads, ", started ", worst_day, ".\n");</
Run as:
<pre>cat readings.txt | tr -d \\r | aime SOURCE_FILE</pre>
Line 227:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!--{{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - argc and argv are extensions}} -->
<
INT no data max := -1; # Max consecutive flags<0 in lines of file #
FLEX[0]STRING no data max line; # ... and line number(s) where it occurs #
Line 321:
upb list := UPB no data max line;
printf(($l"Maximum run"f(p)" of "g(-0)" consecutive false reading"f(p)" ends at line starting with date"f(p)": "$,
upb list = 1, no data max, no data max = 0, upb list = 1, list repr, no data max line, $l$))</
Command:
$ a68g ./Data_Munging.a68 - data
Line 342:
=={{header|AutoHotkey}}==
<
SetFormat, float, 4.2
Line 415:
Totavg := TotSum / TotValid
FileAppend, `n`nDays %Lines%`nMaximal wrong readings: %maxwrong% from %startwrongdate% at %startoccurrence% to %lastwrongdate% at %lastoccurrence%`n`n, %result%
FileAppend, Valid readings: %TotValid%`nTotal Value: %TotSUm%`nAverage: %TotAvg%, %result%</
{{out|Sample output}}
<pre>Day: 1990-01-01 sum: 590.00 avg: 26.82 Readings: 22/24.00
Line 434:
=={{header|AWK}}==
<
nodata = 0; # Current run of consecutive flags<0 in lines of file
nodata_max=-1; # Max consecutive flags<0 in lines of file
Line 496:
printf "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s\n", nodata_max, nodata_maxline
}</
{{out|Sample output}}
<pre>bash$ awk -f readings.awk readings.txt | tail
Line 512:
=={{header|Batch File}}==
<
setlocal ENABLEDELAYEDEXPANSION
set maxrun= 0
Line 578:
echo Line: %date% Accept: %count:~-3% tot: %sum:~-8% avg: %mean:~-8%
goto :EOF</
{{out}}
<pre>
Line 600:
=={{header|BBC BASIC}}==
<
IF file% = 0 THEN PRINT "Could not open test data file" : END
Line 642:
PRINT "Overall mean = " ; Total / Count%
@% = &90A
PRINT '"Longest run of bad readings = " ; BadMax% " ending " BadDate$</
{{out}}
<pre>
Line 665:
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 749:
fclose(outfile);
return 0;
}</
{{out|Sample output}}
<pre>1990-01-01 Reject: 2 Accept: 22 Average: 26.818
Line 766:
=={{header|C++}}==
<
#include <fstream>
#include <string>
Line 832:
cout << "Maximum number of consecutive bad readings is " << badCountMax << endl;
cout << "Ends on date " << badDate << endl;
}</
{{out}}
<pre>1990-01-01 Reject: 2 Accept: 22 Average: 26.818
Line 845:
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
(ns rosettacode.textprocessing1
(:require [clojure.string :as str]))
Line 909:
(count max-gap)
(:date (first max-gap))))))
</syntaxhighlight>
{{out}}
Line 931:
=={{header|COBOL}}==
<
PROGRAM-ID. data-munging.
Line 1,074:
GOBACK
.</
{{Out|Example output}}
Line 1,097:
=={{header|Common Lisp}}==
<
(defvar *max-invalid*)
(defvar *max-invalid-date*)
Line 1,144:
(format t "~%Maximum run(s) of ~a consecutive false readings ends at ~
line starting with date(s): ~a~%"
*max-invalid* *max-invalid-date*)))</
{{out|Example output}}
<pre>...
Line 1,160:
=={{header|D}}==
{{trans|Python}}
<
import std.stdio, std.conv, std.string;
Line 1,234:
"readings ends at line starting with date(s): %-(%s, %)",
noDataMax, noDataMaxLine);
}</
The output matches that of the [[#Python|Python]] version.
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,358:
end
</syntaxhighlight>
{{out}}
Only the last three lines of the per line summary statistics are shown.
Line 1,379:
The function file_contents/1 is used by [[Text_processing/2]]. Please update the user if you make any interface changes.
<syntaxhighlight lang="erlang">
-module( text_processing ).
Line 1,446:
{_Previous, Value_flags} = lists:foldr( fun file_content_line_value_flag/2, {[], []}, Rest ), % Preserve order
{binary:bin_to_list( Date_binary ), Value_flags}.
</syntaxhighlight>
{{out}}
<pre>
Line 1,466:
=={{header|Forth}}==
{{works with|GNU Forth}}
<
\ 1991-03-30[\t10.000\t[-]1]*24
Line 1,558:
total-sum f@ total-n @ .mean cr ;
main bye</
=={{header|Fortran}}==
Line 1,569:
Incidentally, a daily average of a set of measurements may be unsuitable when data are missing, as when there is a regular pattern over a day. The N.Z. electricity supply association ruled that in calculating the ratio of daytime to nighttime usage, should there be four or more missing data in a day, then the entire day's data were to be rejected when computing the monthly or quarterly ratio.
<syntaxhighlight lang="fortran">
Crunches a set of hourly data. Starts with a date, then 24 pairs of value,indicator for that day, on one line.
INTEGER Y,M,D !Year, month, and day.
Line 1,662:
900 CLOSE(IN) !Done.
END !Spaghetti rules.
</syntaxhighlight>
Output:
Line 1,677:
=={{header|Go}}==
<
import (
Line 1,767:
maxRun, maxDate)
}
}</
{{out}}
<pre>
Line 1,785:
=={{header|Haskell}}==
<
import Numeric
import Control.Arrow
Line 1,831:
(\(t,n) -> printf totalFmt n t (t/fromIntegral n)) $ fst summ
mapM_ ((\(l, d1,d2) -> printf maxFmt l d1 d2)
. (\(a,b)-> (a,(fst.(dat!!).(`div`24))b,(fst.(dat!!).(`div`24))(a+b)))) $ snd summ</
{{out}}
<
<pre>Some lines:
Line 1,846:
=={{header|Icon}} and {{header|Unicon}}==
<
procedure main()
Line 1,896:
else
write(fout,"No bad runs of data")
end</
{{out|Sample output}}
<pre>...
Line 1,912:
=={{header|J}}==
'''Solution:'''
<
parseLine=: 10&({. ,&< (_99&".;._1)@:}.) NB. custom parser
summarize=: # , +/ , +/ % # NB. count,sum,mean
Line 1,925:
589
]StartDates=: Dates {~ (>:@I.@e.&MaxRun (24 <.@%~ +/)@{. ]) RunLengths
1993-03-05</
'''Formatting Output'''<br>
Define report formatting verbs:
<
labels=. , ];.2 'Line: Accept: Line_tot: Line_avg: '
labels , x ,. 7j0 10j3 10j3 ": y
Line 1,938:
'maxrun dates'=. x
out=. out,LF,'Maximum run(s) of ',(": maxrun),' consecutive false readings ends at line(s) starting with date(s): ',dates
)</
{{out|Show output}}
<
Line: Accept: Line_tot: Line_avg:
2004-12-28 23 77.800 3.383
Line 1,952:
Average: 10.497
Maximum run(s) of 589 consecutive false readings ends at line(s) starting with date(s): 1993-03-05</
=={{header|Java}}==
{{works with|Java|7}}
<
import java.util.*;
import static java.lang.System.out;
Line 2,044:
}
}
}</
<pre>1990-01-01 out: 2 in: 22 tot: 590.000 avg: 26.818
Line 2,059:
=={{header|JavaScript}}==
{{works with|JScript}}
<
var show_lines = 5;
var file_stats = {
Line 2,127:
function dec3(value) {
return Math.round(value * 1e3) / 1e3;
}</
{{out}}
<pre>Line: 1990-01-01 Reject: 2 Accept: 22 Line_tot: 590 Line_avg: 26.818
Line 2,150:
The "foreach" syntax is:
<
The basic idea is that for each $row in STREAM, the value specified by VALUE is emitted.
If we wished only to produce per-line synopses of the "readings.txt"
file, the following pattern could be used:
<
In order to distinguish the single-line synopsis from the whole-file synopsis, we will use the following pattern instead:
<
The "null" is added so that the stream of per-line values can be distinguished from the last value in the stream.
Line 2,163:
One point of interest in the following program is the use of JSON objects to store values. This allows mnemonic names to be used instead of local variables.
<
# "starts": array_of_start_line_values, # of all the maximal runs
# "start_dates": array_of_start_dates # of all the maximal runs
Line 2,233:
end;
process</
{{out}}
<
[22,590,2]
[24,410,0]
Line 2,242:
[23,47.3,1]
There is one maximal run of lines with flag<=0.
The maximal run has length 93 and starts at line 5378 and has start date 2004-09-30.</
=={{header|Julia}}==
<
using DataFrames
Line 2,300:
maxbaddate = replace("$(df[:Date][maxbadline])", r"T.+$", "")
println("The largest run of bad values is $(maxbadval), on $(maxbaddate) beginning at $(maxbadtime):00 hours.")
</syntaxhighlight>
{{output}}
<pre>
Line 2,335:
=={{header|Kotlin}}==
<
import java.io.File
Line 2,385:
println("\nMaximum run of $maxRun consecutive false readings")
println("ends at line starting with date: $finishLine")
}</
{{out}}
Line 2,412:
=={{header|Lua}}==
<
io.input( filename )
Line 2,460:
print( string.format( "Readings: %d", file_lines ) )
print( string.format( "Average: %f", file_sum/file_cnt_data ) )
print( string.format( "Maximum %d consecutive false readings starting at %s.", max_rejected, max_rejected_date ) )</
<pre>Output:
File: readings.txt
Line 2,469:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Scan[(a=Position[#[[3;;All;;2]],1];
Print["Line:",#[[1]] ,"\tReject:", 24 - Length[a], "\t Accept:", Length[a], "\tLine_tot:",
Line 2,483:
Print["\nFile(s) : ",FileName,"\nTotal : ",AccountingForm@GlobalSum,"\nReadings : ",Nb,
"\nAverage : ",GlobalSum/Nb,"\n\nMaximum run(s) of ",MaxRunRecorded,
" consecutive false readings ends at line starting with date(s):",MaxRunTime]</
<pre>Line:1990-01-01 Reject:2 Accept:22 Line_tot:590. Line_avg:26.8182
Line 2,501:
=={{header|Nim}}==
{{trans|Python}}
<
var
Line 2,553:
echo ""
echo &"Maximum run(s) of {nodataMax} consecutive false readings ",
&"""ends at line starting with date(s): {nodataMaxLine.join(" ")}."""</
{{out}}
Line 2,569:
=={{header|OCaml}}==
<
try Some(input_line ic)
with End_of_file -> None
Line 2,635:
Printf.printf "Maximum run(s) of %d consecutive false readings \
ends at line starting with date(s): %s\n"
nodata_max (String.concat ", " nodata_maxline);</
=={{header|Perl}}==
===An AWK-like solution===
<
use warnings;
Line 2,697:
printf "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s\n",
$nodata_max, $nodata_maxline;</
{{out|Sample output}}
<pre>bash$ perl -f readings.pl readings.txt | tail
Line 2,713:
===An object-oriented solution===
<
use warnings;
Line 2,827:
$parser->_push_bad_range_if_necessary
}
}</
{{out|Sample output}}
<pre>$ perl readings.pl < readings.txt | tail
Line 2,844:
=={{header|Phix}}==
<!--<
<span style="color: #000080;font-style:italic;">-- demo\rosetta\TextProcessing1.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (include version/first of next three lines only)</span>
Line 2,928:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 2,940:
=={{header|Picat}}==
{{trans|Ruby}}
<
File = "readings.txt",
Total = new_map([num_readings=0,num_good_readings=0,sum_readings=0.0]),
Line 2,989:
println(invalidRunEnd=InvalidRunEnd),
nl.</
{{out}}
Line 3,006:
{{trans|AWK}}
Put the following into an executable file "readings":
<
(let (NoData 0 NoDataMax -1 NoDataMaxline "!" TotFile 0 NumFile 0)
Line 3,048:
" consecutive false readings ends at line starting with date(s): " NoDataMaxline ) ) )
(bye)</
Then it can be called as
<pre>$ ./readings readings.txt |tail
Line 3,064:
=={{header|PL/I}}==
<
declare line character (2000) varying;
Line 3,117:
finish_up:
end text1;</
=={{header|PowerShell}}==
<
$lines = Get-Content $file # $args[0]
$valid = $true
Line 3,173:
"Consecutive false readings starts at line starting with date $startDate at hour {0:0#}:00." -f $startHour
"Consecutive false readings ends at line starting with date $endDate at hour {0:0#}:00." -f $endHour
}</
<pre>Line : 2004-12-29
Reject : 1
Line 3,203:
=={{header|PureBasic}}==
<
Define File$, InLine$, Part$, i, Out$, ErrEnds$, Errcnt, ErrMax
Define lsum.d, tsum.d, rejects, val.d, readings
Line 3,249:
;
Print("Press ENTER to exit"): Input()
EndIf</
{{out|Sample output}}
<pre>...
Line 3,266:
=={{header|Python}}==
<
import sys
Line 3,321:
print "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s" % (
nodata_max, ", ".join(nodata_maxline))</
{{out|Sample output}}
<pre>bash$ /cygdrive/c/Python26/python readings.py readings.txt|tail
Line 3,337:
=={{header|R}}==
<
dfr <- read.delim("readings.txt")
#Calculate daily means
Line 3,345:
#Calculate time between good measurements
times <- strptime(dfr[1,1], "%Y-%m-%d", tz="GMT") + 3600*seq(1,24*nrow(dfr),1)
hours.between.good.measurements <- diff(times[t(flags)])/3600</
=={{header|Racket}}==
<
;; Use SRFI 48 to make %n.nf formats convenient.
(require (prefix-in srfi/48: srfi/48)) ; SRFI 48: Intermediate Format Strings
Line 3,401:
(unless (zero? N) (srfi/48:format #t "Average = ~10,3F~%" (/ sum N)))
(srfi/48:format #t "~%Maximum run(s) of ~a consecutive false readings ends at line starting with date(s): ~a~%"
max-consecutive-false (string-join max-false-tags))))</
{{out|Sample run}}
<pre>$ racket 1.rkt readings/readings.txt | tail
Line 3,417:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my $previous = 'valid';
Line 3,449:
say "Longest period of invalid readings was {$longest<count>} hours,\n",
"from {$longest<start>} till {$longest<end>}."</
{{out}}
<pre>
Line 3,470:
=={{header|REXX}}==
<
numeric digits 20 /*allow for bigger (precision) numbers.*/
ifid='READINGS.TXT' /*the name of the input file. */
Line 3,534:
do j=e to b by -3; _=insert(',',_,j); end /*j*/; return _
/*────────────────────────────────────────────────────────────────────────────*/
sy: say arg(1); call lineout ofid,arg(1); return</
'''output''' when using the default input file:
<pre style="height:40ex">
Line 3,557:
=={{header|Ruby}}==
<
total = { "num_readings" => 0, "num_good_readings" => 0, "sum_readings" => 0.0 }
invalid_count = 0
Line 3,599:
printf "Average = %.3f\n", total['sum_readings']/total['num_good_readings']
puts ""
puts "Maximum run(s) of #{max_invalid_count} consecutive false readings ends at #{invalid_run_end}"</
Alternate implementation:
<
DailyReading = Struct.new(:date, :readings) do
Line 3,634:
puts
puts "Max run of #{worst_streak.count} consecutive false readings from #{worst_streak.first.date} until #{worst_streak.last.date}"
</syntaxhighlight>
=={{header|Scala}}==
Line 3,640:
A fully functional solution, minus the fact that it uses iterators:
<
import scala.io.Source
Line 3,697:
println(report format (files mkString " ", totalSum, totalSize, totalSum / totalSize, invalidCount, startDate))
}
}</
A quick&dirty solution:
<
def main(args: Array[String]) {
var totalSum = 0.0
Line 3,740:
println(report format (files mkString " ", totalSum, totalSize, totalSum / totalSize, maxInvalidCount, maxInvalidDate))
}
}</
Last few lines of the sample output (either version):
<pre>
Line 3,758:
=={{header|Sidef}}==
{{trans|Raku}}
<
var previous = :valid;
Line 3,790:
say ("Longest period of invalid readings was #{longest{:count}} hours,\n",
"from #{longest{:start}} till #{longest{:end}}.");</
{{out}}
<pre>
Line 3,808:
{{trans|Rust}}
<
let fmtDbl = { String(format: "%10.3f", $0) }
Line 3,888:
dispatchMain()
</syntaxhighlight>
{{out}}
Line 3,904:
=={{header|Tcl}}==
<
set max_invalid_run_end ""
set tot_file 0
Line 3,944:
puts "Average = [format %.3f [expr {$tot_file / $num_file}]]"
puts ""
puts "Maximum run(s) of $max_invalid_run consecutive false readings ends at $max_invalid_run_end"</
=={{header|Ursala}}==
Line 3,950:
and booleans (type <code>%ebXLm</code>) in the parsed data. The same function is used to compute the daily and the
cumulative statistics.
<
#import nat
#import flo
Line 3,970:
@nmrSPDSL -&~&,leql$^; ^/length ~&zn&-@hrZPF+ rlc both ~&rZ+-
main = ^T(daily_stats^lrNCT/~& @mSL 'summary ':,long_run) parsed_data</
last few lines of output:
<pre>
Line 3,981:
=={{header|VBScript}}==
<
Set objFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
"\data.txt",1)
Line 4,043:
WScript.StdOut.WriteLine
objFile.Close
Set objFSO = Nothing</
{{Out}}
<pre>
Line 4,059:
{{trans|AWK}}
Vedit does not have floating point data type, so fixed point calculations are used here.
<
File_Open("output.txt")
#51 = Buf_Num // Edit buffer for output file
Line 4,126:
IT("Maximum run(s) of ") Num_Ins(#13, LEFT+NOCR)
IT(" consecutive false readings ends at line starting with date(s): ") Reg_Ins(15)
IN</
{{out|Sample output}}
<pre>
Line 4,145:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<
import "/pattern" for Pattern
import "/fmt" for Fmt
Line 4,198:
Fmt.print("Average = $0.3f", average)
Fmt.print("\nMaximum run of $d consecutive false readings", maxRun)
Fmt.print("ends at line starting with date: $s", finishLine)</
{{out}}
|