Text processing/1: Difference between revisions

→‎{{header|J}}: break up code and usage
No edit summary
(→‎{{header|J}}: break up code and usage)
Line 511:
 
=={{header|J}}==
<lang j>
NB. Compares all-at-once and block processing methods
 
NB. Compares all-at-once and block processing methods
require 'files' NB. for fapplylines adverb
 
First load <tt>files</tt> library and define some utility verbs for use in all examples.
<lang j>
require 'files' NB. for fread/freadblock/fapplylines adverb
 
NB. Utility verbs
Line 525 ⟶ 527:
badflags=. , 0 >: y
 
NB. define local utility verbs local to this verb
getrunlengths=. [: #(;.1) 0 , }. *. }:
getidxmax=. >:@I.@e. >./
Line 548 ⟶ 550:
out=. out,LF,'Maximum run(s) of ',(": maxrun),' consecutive false readings ends at line(s) starting with date(s): ',dates
)
</lang>
 
NB. Process whole file at once
 
NB. Using addon package to parse file
 
Note 'Example session'
<lang j>
load 'tables/dsv' NB. use tables/dsv addon
dat=: TAB readdsv jpath '~temp/readings.txt'
Line 561 ⟶ 565:
MaxRuns=: findLongestRuns flags
(_4{.Dates) formatDailySumry _4{. DailySumry
NB. add output
MaxRuns formatFileSumry DailySumry
NB. add output
)
</lang>
 
NB. Build custom parser instead of using addon package
 
<lang j>
NB. Processing verbs
parseLine=: 10&({. ,&< (_99&".;._1)@:}.)
Line 577 ⟶ 584:
dates;sumry;flags NB. return block of dates and summaries
)
</lang>
 
Note 'Exampe usage'
<lang j>
'Dates DailySumry'=: processBlock fread jpath '~temp/readings.txt'
(_4{.Dates) formatDailySumry _4{. DailySumry
NB. add output
formatFileSumry DailySumry
NB. add output
)
</lang>
 
NB. Process file in blocks
NB. For huge files it might be desireable to read/process the file in blocks
 
NB. Example using freadblock to process blocks of lines
<lang j>
mungeDataBlocks=: monad define
sz=. fsize y
Line 608 ⟶ 620:
Dates;DailySumry;Flags
)
</lang>
 
Note 'Example usage'
<lang j>
'Dates DailySumry Flags'=: mungeDataBlocks jpath '~temp/readings.txt'
NB. add output
$ each Dates;DailySumry;Flags
NB. add output
)
</lang>
 
 
NB. Example using fapplylines to process a line at a time.
<lang j>
processLine=: monad define
'dates dat'=. parseLine y
Line 641 ⟶ 657:
smoutput MaxRuns formatFileSumry DailySumry
)
</lang>
 
Note 'Example usage'
<lang j>
mungeDataLines jpath '~temp/readings.txt'
NB. add output
$ each Dates;DailySumry
NB. add output
)
 
</lang>
 
892

edits