SCAN / Find Position in file



Software AG's platform-independent programming language with full support for open-source and Internet applications

SCAN / Find Position in file

Postby newjb » Wed Oct 03, 2012 8:35 pm

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.
newjb
 
Posts: 20
Joined: Fri Sep 07, 2012 11:25 pm
Has thanked: 3 times
Been thanked: 0 time

Re: SCAN / Find Position in file

Postby RGZbrog » Thu Oct 04, 2012 7:19 pm

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
User avatar
RGZbrog
 
Posts: 101
Joined: Mon Nov 23, 2009 1:34 pm
Location: California, USA
Has thanked: 0 time
Been thanked: 0 time

Re: SCAN / Find Position in file

Postby newjb » Wed Oct 10, 2012 5:45 am

This worked perfect. Thank you :)
newjb
 
Posts: 20
Joined: Fri Sep 07, 2012 11:25 pm
Has thanked: 3 times
Been thanked: 0 time


Return to Natural

 


  • Related topics
    Replies
    Views
    Last post