Task: Text processing_1








VB: Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
			"\data.txt",1)

bad_readings_total = 0
good_readings_total = 0
data_gap = 0
start_date = ""
end_date = ""
tmp_datax_gap = 0
tmp_start_date = ""

Do Until objFile.AtEndOfStream
	bad_readings = 0
	good_readings = 0
	line_total = 0
	line = objFile.ReadLine
	token = Split(line,vbTab)
	n = 1
	Do While n <= UBound(token)
		If n + 1 <= UBound(token) Then
			If CInt(token(n+1)) < 1 Then
				bad_readings = bad_readings + 1
				bad_readings_total = bad_readings_total + 1
				
				If tmp_start_date = "" Then
					tmp_start_date = token(0)
				End If
				tmp_data_gap = tmp_data_gap + 1
			Else
				good_readings = good_readings + 1
				line_total = line_total + CInt(token(n))
				good_readings_total = good_readings_total + 1
				
				If (tmp_start_date <> "") And (tmp_data_gap > data_gap) Then
					start_date = tmp_start_date
					end_date = token(0)
					data_gap = tmp_data_gap
					tmp_start_date = ""
					tmp_data_gap = 0
				Else
					tmp_start_date = ""
					tmp_data_gap = 0
				End If
			End If	
		End If
		n = n + 2
	Loop
	line_avg = line_total/good_readings
	WScript.StdOut.Write "Date: " & token(0) & vbTab &_
		"Bad Reads: " & bad_readings & vbTab &_
		"Good Reads: " & good_readings & vbTab &_
		"Line Total: " & FormatNumber(line_total,3) & vbTab &_
		"Line Avg: " & FormatNumber(line_avg,3)
	WScript.StdOut.WriteLine
Loop
WScript.StdOut.WriteLine
WScript.StdOut.Write "Maximum run of " & data_gap &_ 
	" consecutive bad readings from " & start_date & " to " &_
	end_date & "."
WScript.StdOut.WriteLine
objFile.Close
Set objFSO = Nothing



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		
      INTEGER GOOD(24)		
      REAL*8 V(24),VTOT,T	
      INTEGER NV,N,NB		
      INTEGER I,NREC,HIC	
      INTEGER BI,BN,BBI,BBN	
      CHARACTER*10 BDATE,BBDATE	
      LOGICAL INGOOD		
      INTEGER IN,MSG		
      CHARACTER*666 ACARD	
      IN = 10		
      MSG = 6		
      OPEN (IN,FILE="Readings1.txt", FORM="FORMATTED",	
     1 STATUS ="OLD",ACTION="READ")			
      NB = 0		
      NV = 0		
      VTOT = 0		
      NREC = 0		
      HIC = 0		
      INGOOD = .TRUE.	
      BBN = 0		
Chew into the file.
   10 READ (IN,11,END=100,ERR=666) L,ACARD(1:MIN(L,LEN(ACARD)))	
      NREC = NREC + 1		
   11 FORMAT (Q,A)		
      READ (ACARD,12,END=600,ERR=601) Y,M,D	
   12 FORMAT (I4,2(1X,I2))				
      READ (ACARD(11:L),*,END=600,ERR=601) (V(I),GOOD(I),I = 1,24)	
Calculations. Could use COUNT(array) and SUM(array), but each requires its own pass through the array.
   20 T = 0		
      N = 0		
      DO I = 1,24	
        IF (GOOD(I).GT.0) THEN	
          N = N + 1		
          T = T + V(I)		
          IF (.NOT.INGOOD) THEN	
            INGOOD = .TRUE.	
            IF (BN.GT.BBN) THEN	
              BBN = BN		
              BBI = BI		
              BBDATE = BDATE	
            END IF		
          END IF		
         ELSE		
          IF (INGOOD) THEN	
            INGOOD = .FALSE.	
            BDATE = ACARD(1:10)	
            BI = I		
            BN = 0		
          END IF		
          BN = BN + 1	
        END IF		
      END DO		
Commentary for the day's data..
      IF (N.LE.0) THEN	
        WRITE (MSG,21) NREC,ACARD(1:10)	
   21   FORMAT ("Record",I8," (",A,") has no good data
       ELSE			
        WRITE(MSG,22) NREC,ACARD(1:10),N,T/N	
   22   FORMAT("Record",I8," (",A,")",I3," good, average",F9.3)	
        NB = NB + 24 - N	
        NV = NV + N		
        VTOT = VTOT + T		
      END IF			
      GO TO 10		

Complaints. Should really distinguish between trouble in the date part and in the data part.
  600 WRITE (MSG,*) '"END" declared - insufficient data?'	
      GO TO 602				
  601 WRITE (MSG,*) '"ERR" declared - improper number format?'	
  602 WRITE (MSG,603) NREC,L,ACARD(1:L)	
  603 FORMAT(" Record ",I0,", length ",I0," reads ",A)	
      HIC = HIC + 1			
      IF (HIC.LE.12) GO TO 10		
      STOP "Enough distaste."		
  666 WRITE (MSG,101) NREC,"format error
      GO TO 900				

Closedown.
  100 WRITE (MSG,101) NREC,"then end-of-file"	
  101 FORMAT (" Record ",I0,": ",A)		
      WRITE (MSG,102) NV,NB,VTOT/NV		
  102 FORMAT (I8," values, ",I0," bad. Average",F9.4)	
      IF (BBN.LE.0) THEN		
        WRITE (MSG,*) "No bad value presented, so no longest run."	
       ELSE				
        WRITE (MSG,103) BBN,BBI,BBDATE	
  103   FORMAT ("Longest bad run: ",I0,", starting hour ",I0," on ",A)	
      END IF			
  900 CLOSE(IN)		
      END