INSTR-Func

 

 

NOTE - Use your Browsers BACK Button to return to prior page or CLICK here.

   INSTR-Func - Bi-Directional INSTR Function
Zalligator.jpg (24179 bytes) SQR has a function called INSTR that only works in one direction - forwards. My INSTR-Func routine was designed to work both forwards and backwards... this means you can search for a pattern in a string starting from the end and traversing backwards (towards the beginning of the string)... The function works exactly as INSTR with the exception of the direction parameter...

You can find the INSTR-Func function in my custom SQR library TDFUNC.SQC

NOTE:
The alligator (top) and hippo (bottom) drawings are part of a collection I'm
creating for my daughter... The theme is zoo animals... the sketchbook I'm using
is 100 pages... As of today I only have 85 more pages to go! You'll see some (if
not all) of them on this site! I'm taking a simplistic approach, softening each
creatures appearance while maintaining some anatomical accuracy... such as the
"happy" looking alligators... another goal is to place the characters in warm,
inviting settings with lush plants and cascading streams... special thanks goes to
Robin Artis (from my current client site) - she gave me the idea of adding the
butterflies... click on the pictures for enlarged versions...

   INSTR-Func - Input / Output Parameters
 
Input    - Direction (B=Backward, F=Forward)
Input    - Input String
Input    - Search String (substring)
Input    - Starting Search Position
Output  - Position in String (or ZERO)

For information on the SQR INSTR() function refer to the SQR manual or the book "SQR in PeopleSoft and Other Applications" by Galina and Vlad Landres.

EXAMPLE:

let $path = ''
let $name = ''
let $file = '/usr/tmp/myfile.txt'
let #len  = length($file)

do INSTR-Func('B', $file, '/', #len, #pos)

if #pos   > 0
   let $path = substr($file, 1, #pos)
   let $name = substr($file, #pos + 1, #len - #pos)
end-if

show 'Path/Filename: ' $file
show '         Path: ' $path
show '     Filename: ' $name
                                        

The INSTR-Func routine will return the position of the first '/' starting from the end of the string (going backwards)... This position is then used to parse the PATH and FILENAME from the $file variable...

   INSTR-Func - Source Code
!**********************************************************************
!*       Find Pattern in String                                       *
!**********************************************************************
!*                                                                    *
!*        INPUT: $I_dir     - Direction B=Bwd F=Fwd [Default]         *
!*               $I_string  - Input String                            *
!*               $I_find    - Search String                           *
!*               #I_pos     - Start Position                          *
!*       OUTPUT: #O_pos     - Position in String (or ZERO)            *
!*                                                                    *
!*               To de-activate this function:                        *
!*               #define INSTR_Func_Remove                            *
!*                                                                    *
!**********************************************************************
!*                                                                    *
!*      EXAMPLE: do INSTR-Func('B', 'HELLO, WORLD', 'L', 12, #O_pos1) *
!*               do INSTR-Func('B', 'HELLO, WORLD', 'L', 10, #O_pos2) *
!*               do INSTR-Func('F', 'HELLO, WORLD', 'W',  1, #O_pos3) *
!*                                                                    *
!*      RESULTS: #O_pos1  = 11 ('L' found (backwards) from pos.12).   *
!*               #O_pos2  =  4 ('L' found (backwards) from pos.10).   *
!*               #O_pos3  =  8 ('W' found (forwards)  from pos. 1).   *
!*                                                                    *
!**********************************************************************

#ifndef INSTR_Func_Remove

begin-procedure INSTR-Func($I_dir, $I_string, $I_find, #I_pos, :#O_pos)

let #O_pos         = 0

let #len           = length($I_string)
let #adj           = length($I_find)
let #pos           = #I_pos

let #dir           = 1
if  $I_dir         = 'B'
    let #dir       = -1
end-if

while #pos         > 0
  and #pos        <= #len

    let $chars     = substr($I_string, #pos, #adj)

    if  $chars     = $I_find
        let #O_pos = #pos
        break
    end-if

    let #pos       = #pos + #dir

end-while

end-procedure

#endif

!**********************************************************************
                                                           

   Feedback
ftoct01.jpg (12389 bytes) I would appreciate any feedback you may have on this site. Send mail to tdelia@erols.com or click on the Octopus.

   Technical difficulties?
Zhippo.jpg (18571 bytes) Please report any technical difficulties you may encounter to the address above OR click on the Octopus. Thanks.

NOTE - Use your Browsers BACK Button to return to prior page.

Tony DeLia - Updated June 03, 2000     Beluga.GIF (1101 bytes)