Page 1 of 1

SCAN / Find Position in file

PostPosted: Wed Oct 03, 2012 8:35 pm
by newjb
I am using the SCAN statement to find a value within a 1000 Byte record. For example:

(#INPUT-FILE is 1000 byte record)

IF #INPUT-FILE = SCAN '12LTU3'
ADD +1 TO #COUNTER
WRITE(1) #TR-DATE ' ' #TR-ACCTNO ' ' #TR-ACCTNAME
END-IF

WRITE(1) 'TOTAL RECORDS FOUND' #COUNTER

My question is... is there a way to write out the position spot where the record was found? Say it found '12LTU3' in position 152 of 1000. The program would write out 152.

Thank you in advance.

Re: SCAN / Find Position in file

PostPosted: Thu Oct 04, 2012 7:19 pm
by RGZbrog
SCAN is simply an option of the IF statement, rather than a System Function. All it can do is answer the question "Does the string exist in the source variable?"

The EXAMINE statement can do what you need.

DEFINE DATA LOCAL
1 #INPUT-FILE (A1000) INIT <'  12LTU3'>
1 #COUNTER (I4)
1 #TR-DATE (D)        INIT <*DATX'>
1 #TR-ACCTNO (N3)     INIT <123>
1 #TR-ACCTNAME (A5)   INIT <'Zbrog'>
1 #P (I4)
END-DEFINE
EXAMINE #INPUT-FILE FOR '12LTU3' GIVING POSITION #P
IF  #P > 0
  THEN
    ADD 1 TO #COUNTER
    WRITE (0) #TR-DATE
              #TR-ACCTNO
              #TR-ACCTNAME
END-IF
WRITE (0) 'TOTAL RECORDS FOUND' #COUNTER
END
And here is the result:
Page     1                                                   10/04/12  06:37:50
 
10/03/12  123 Zbrog
TOTAL RECORDS FOUND           1


Off topic: The blanks in your WRITE statement could be confusing. Natural automatically inserts a space between items in the list, so you were separating your variables by 3 (space-' '-space-#TR-ACCTNO). If that was intended, try
    WRITE (0) #TR-DATE
           3X #TR-ACCTNO
           3X #TR-ACCTNAME
Page     1                                                   10/04/12  06:45:26
 
10/04/12    123   Zbrog
TOTAL RECORDS FOUND           1

Re: SCAN / Find Position in file

PostPosted: Wed Oct 10, 2012 5:45 am
by newjb
This worked perfect. Thank you :)