    PROGRAM m01nbfe

!      M01NBF Example Program Test

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : m01nbf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: i, ifail, index, ioerr, item, m1, m2
       LOGICAL                         :: first
!      .. Local Arrays ..
       INTEGER, ALLOCATABLE            :: iv(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'M01NBF Example Program Results'

!      Skip heading in data file
       READ (nin,*)

       READ (nin,*) m2
       ALLOCATE (iv(m2))

       m1 = 1

       READ (nin,*) (iv(i),i=m1,m2)

       first = .TRUE.

DATA:  DO
          READ (nin,*,IOSTAT=ioerr) item

          IF (ioerr<0) THEN
             EXIT DATA
          END IF

          ifail = 0
          index = m01nbf(first,iv,m1,m2,item,ifail)

          IF (first) THEN
             WRITE (nout,*)
             WRITE (nout,*) 'Reference Vector is:'
             WRITE (nout,99999) (iv(i),i=m1,m2)
             first = .FALSE.
          END IF

          WRITE (nout,*)
          WRITE (nout,99998) item, index
       END DO DATA

99999  FORMAT (1X,8I5)
99998  FORMAT (1X,'Search for item ',I5,' returned index: ',I4)
    END PROGRAM m01nbfe
