× 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.



If the array is ordered (sequenced) you can use LOOKUP with the GT/EQ indicators
XOR use the %LOOKUPGE() built-in function. That may do what you want.  

-Bob Cozzi
www.iSeriesTV.com
Ask your Manager to watch iSeriesTV.com

-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx [mailto:rpg400-l-bounces@xxxxxxxxxxxx] On
Behalf Of Lim Hock-Chai
Sent: Tuesday, August 22, 2006 9:03 AM
To: RPG programming on the AS400 / iSeries
Subject: RE: Bsearch for closest match

Thanks Scott.  

For the example that I've (which you mentioned that it could be elem 2,3
or,4), is there a pattern on which elem it will pick?

If this array is a physical file and I did a setll and then readp, I
would expect it to always pick elem 4.

-----Original Message-----
From: rpg400-l-bounces@xxxxxxxxxxxx
[mailto:rpg400-l-bounces@xxxxxxxxxxxx] On Behalf Of Scott Klement
Sent: Monday, August 21, 2006 5:53 PM
To: RPG programming on the AS400 / iSeries
Subject: Re: Bsearch for closest match


Bsearch() has to find an exact match on something.  It can find an exact
match on PART of your key, but, some part of it must match exactly.

In your example, you have this:

Array(1) = 'AB'
Array(2) = 'ABC'
Array(3) = 'ABCD'
Array(4) = 'ABCDEF'
Array(5) = 'ACCDEF'

Search key is ABCE123.

For this example, you could search for the first 3 letters (ABC) to
match. 
When you found a match (which could be any of elemnts 2, 3 or 4) you
could then read backward and forward in the array to find the "closest"
match.

However, what if you had this?

Array(1) = AAAAAAA
Array(2) = BBBBBBB
Array(3) = CCCCCCC
Array(4) = FFFFFFF
Array(5) = GGGGGGG
Array(6) = HHHHHHH

Search key is DEFGHIJ

In this case, I'd think you'd want #4 to be the "closest match" (sort
of, I guess) since it's the next higher element.  But you can't do that
with
bsearch() because there's no part of the element that matches exactly.

Instead of using bsearch(), you might be better off writing your own
binary search tool.  It would do this:

a) Array has 6 elements... so check element 3.
b) Is key > element 3?  Yes.  We have elemnts 4,5,6 left, so try 5.
c) Is key > element 5?  No.
d) IS key < element 5?  YEs.  We have element 4 left.
e) Is key < element 4?  Yes.  Is key > element 3?  Yes. Closest match is
element 4.

Something like that...

--
Scott Klement  http://www.scottklement.com

On Mon, 21 Aug 2006, Lim Hock-Chai wrote:

I've a dynamic array that I need to do bsearch with closest prefix 
matched element. Does anybody has an example on how to that?

Eample:
Array(1) = 'AB'
Array(2) = 'ABC'
Array(3) = 'ABCD'
Array(4) = 'ABCDEF'
Array(5) = 'ACCDEF'

if search argument is ABCE123 ==> The closest match would be elem 2.

thanks.

--
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing 
list To post a message email: RPG400-L@xxxxxxxxxxxx To subscribe, 
unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx Before posting, please take a 
moment to review the archives at http://archive.midrange.com/rpg400-l.

--
This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing
list To post a message email: RPG400-L@xxxxxxxxxxxx To subscribe,
unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
http://archive.midrange.com/rpg400-l.



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.