× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi,

>
> I've been fighting with this same issue. Does anyone have an RPG Prototype
> for the bsearch() c function. I think I can wrap that in a service program.
>

Really?  I pretty much always load my arrays in order.  (In other words, I
first load element 1, then element 2, etc..) I don't usually have to
search the array to find the next unused one, since all I need is a
counter.

The other thing...  MODS are REALLY AWKWARD to use with qsort() or
bsearch() since you can't really use them as parameters.  You have to
re-define the data structure each time you want to use them.  That just
drives me nuts.

I really think a qualified DS array is much more elegant.  The following
sample code shows how LIKEDS makes it soooo much nicer than the
alternatives:

     H DFTACTGRP(*NO) BNDDIR('QC2LE')

     D qsort           PR                  extproc('qsort')
     D   base                          *   value
     D   num                         10U 0 value
     D   width                       10U 0 value
     D   compare                       *   procptr value

     D bsearch         PR              *   extproc('bsearch')
     D   key                           *   value
     D   base                          *   value
     D   num                         10U 0 value
     D   size                        10U 0 value
     D   compare                       *   procptr value

     D myTemplate      ds                  qualified
     D                                     based(Template)
     D   LastName                    20A
     D   FirstName                   20A
     D   ext                         10I 0

     D users           ds                  likeds(myTemplate)
     D                                     dim(100)

     D p_match         s               *
     D match           ds                  likeds(myTemplate)
     D                                     based(p_match)

     D key             ds                  likeds(myTemplate)

     D CompByLast      pr            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)

     D CompByFirst     pr            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)

     D CompByExt       pr            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)

     D CompCase        PR            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)

     D x               s             10I 0
     D numUsers        s             10I 0
     D msg             s             52A

      /free

         // -------------------------------------------
         // create some sample data
         // -------------------------------------------

          x = 1;
          users(x).LastName  = 'Klement';
          users(x).FirstName = 'Scott';
          users(x).ext       = 292;

          x = x + 1;
          users(x).LastName  = 'Lewis';
          users(x).FirstName = 'Doug';
          users(x).ext       = 280;

          x = x + 1;
          users(x).LastName  = 'Bizub';
          users(x).FirstName = 'James';
          users(x).ext       = 291;

          x = x + 1;
          users(x).LastName  = 'Michuda';
          users(x).FirstName = 'Michael';
          users(x).ext       = 209;

          x = x + 1;
          users(x).LastName  = 'Solano';
          users(x).FirstName = 'Maria';
          users(x).ext       = 216;

          x = x + 1;
          users(x).LastName  = 'Straw';
          users(x).FirstName = 'Penny';
          users(x).ext       = 302;

          x = x + 1;
          users(x).LastName  = 'Wiesner';
          users(x).FirstName = 'Beatrice';
          users(x).ext       = 200;

          x = x + 1;
          users(x).LastName  = 'Vogl';
          users(x).FirstName = 'Jackie';
          users(x).ext       = 201;

          x = x + 1;
          users(x).LastName  = 'Sotski';
          users(x).FirstName = 'Daniel';
          users(x).ext       = 203;

          numUsers = x;


         // -------------------------------------------
         // Sort array by Last name
         // -------------------------------------------

         qsort( %addr(users)
              : numUsers
              : %size(myTemplate)
              : %paddr(CompByLast) );

         // -------------------------------------------
         // Search for 'Klement'
         //   then for 'Michuda'
         // -------------------------------------------

         key.LastName = 'Klement';

         p_match = bsearch( %addr(key)
                          : %addr(users)
                          : numUsers
                          : %size(myTemplate)
                          : %paddr(CompByLast) );

         if (p_match = *NULL);
             msg = %trimr(key.lastname) + ' not found!';
             dsply msg;
         else;
             msg = %trimr(match.lastname) + ' is ext '
                 + %char(match.ext);
             dsply msg;
         endif;

         key.LastName = 'Michuda';

         p_match = bsearch( %addr(key)
                          : %addr(users)
                          : numUsers
                          : %size(myTemplate)
                          : %paddr(CompByLast) );

         if (p_match = *NULL);
             msg = %trimr(key.lastname) + ' not found!';
             dsply msg;
         else;
             msg = %trimr(match.lastname) + ' is ext '
                   + %char(match.ext);
             dsply msg;
         endif;


         // -------------------------------------------
         // How about searching by complete name
         //   (first & last)
         // -------------------------------------------

         qsort( %addr(users)
              : numUsers
              : %size(myTemplate)
              : %paddr(CompByFirst) );

         key.FirstName = 'Scott';
         key.LastName = 'Klement';

         p_match = bsearch( %addr(key)
                          : %addr(users)
                          : numUsers
                          : %size(myTemplate)
                          : %paddr(CompByFirst) );

         if (p_match = *NULL);
             msg = %trimr(key.firstname) + ' '
                 + %trimr(key.lastname) + ' not found!';
             dsply msg;
         else;
             msg = %trimr(match.firstname) + ' '
                 + %trimr(match.lastname) + ' is ext '
                 + %char(match.ext);
             dsply msg;
         endif;


         // -------------------------------------------
         // How about searching by extension number
         // -------------------------------------------

         qsort( %addr(users)
              : numUsers
              : %size(myTemplate)
              : %paddr(CompByExt) );

         key.ext = 291;

         p_match = bsearch( %addr(key)
                          : %addr(users)
                          : numUsers
                          : %size(myTemplate)
                          : %paddr(CompByExt) );

         if (p_match = *NULL);
             msg = %char(key.ext) + ' not found!';
             dsply msg;
         else;
             msg = %trimr(match.firstname) + ' '
                 + %trimr(match.lastname) + ' is ext '
                 + %char(match.ext);
             dsply msg;
         endif;

         // -------------------------------------------
         //  You can also do a case-insensitive sort
         //   and search just by changing the
         //   way the elements are compared
         // -------------------------------------------

         qsort( %addr(users)
              : numUsers
              : %size(myTemplate)
              : %paddr(CompCase) );

         key.LastName = 'stRaW';

         p_match = bsearch( %addr(key)
                          : %addr(users)
                          : numUsers
                          : %size(myTemplate)
                          : %paddr(CompCase) );

         if (p_match = *NULL);
             msg = %trimr(key.lastname) + ' not found!';
             dsply msg;
         else;
             msg = %trimr(match.firstname) + ' '
                 + %trimr(match.lastname) + ' is ext '
                 + %char(match.ext);
             dsply msg;
         endif;

         *inlr = *on;

      /end-free


      *++++++++++++++++++++++++++++++++++++++++++++++++++++
      * Compare Two Elements, using Last Name as the
      *       only key.
      *++++++++++++++++++++++++++++++++++++++++++++++++++++
     P CompByLast      B
     D CompByLast      PI            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)
      /free

          select;
          when (elem1.LastName < elem2.LastName);
             return -1;
          when (elem1.LastName > elem2.LastName);
             return 1;
          other;
             return 0;
          endsl;

      /end-free
     P                 E


      *++++++++++++++++++++++++++++++++++++++++++++++++++++
      * Compare Two Elements, using a composite key
      *     created from the first & last name
      *++++++++++++++++++++++++++++++++++++++++++++++++++++
     P CompByFirst     B
     D CompByFirst     PI            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)
      /free

          select;
          when (elem1.FirstName < elem2.FirstName);
             return -1;
          when (elem1.FirstName > elem2.FirstName);
             return 1;
          when (elem1.LastName < elem2.LastName);
             return -1;
          when (elem1.LastName > elem2.LastName);
             return 1;
          other;
             return 0;
          endsl;

      /end-free
     P                 E


      *++++++++++++++++++++++++++++++++++++++++++++++++++++
      * Compare Two Elements, using the telephone ext
      *     as the key
      *++++++++++++++++++++++++++++++++++++++++++++++++++++
     P CompByExt       B
     D CompByExt       PI            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)
      /free

          select;
          when (elem1.Ext < elem2.ext);
             return -1;
          when (elem1.Ext > elem2.ext);
             return 1;
          other;
             return 0;
          endsl;

      /end-free
     P                 E


      *++++++++++++++++++++++++++++++++++++++++++++++++++++
      * Compare Two Elements, using the telephone ext
      *     as the key
      *++++++++++++++++++++++++++++++++++++++++++++++++++++
     P CompCase        B
     D CompCase        PI            10I 0
     D   elem1                             likeds(myTemplate)
     D   elem2                             likeds(myTemplate)

     D upper           c                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
     D lower           c                   'abcdefghijklmnopqrstuvwxyz'

     D last1           s                   like(myTemplate.lastname)
     D last2           s                   like(myTemplate.lastname)
      /free

          last1 = %xlate(lower:upper: elem1.lastname);
          last2 = %xlate(lower:upper: elem2.lastname);

          select;
          when (last1 < last2);
             return -1;
          when (last1 > last2);
             return 1;
          other;
             return 0;
          endsl;

      /end-free
     P                 E


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.