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



Scott:

You are correct Template was always *null, so it was not working.  I tried 
some variations of it & had no luck. 

The second eval works just like I need.

Thanks for your, and everyone else who posted,  help. 


Steve Jones



Scott Klement <rpg400-l@xxxxxxxxxxxxxxxx> 
Sent by: rpg400-l-bounces@xxxxxxxxxxxx
11/01/2005 01:19 PM
Please respond to
RPG programming on the AS400 / iSeries <rpg400-l@xxxxxxxxxxxx>


To
RPG programming on the AS400 / iSeries <rpg400-l@xxxxxxxxxxxx>
cc

Subject
Re: Bsearch







Hi Steve,

[SNIP]
>     C                   Eval      Elem = (( P_match - Template)
>     C                                    / %Size(MyTemplate)) + 1
[SNIP]

Won't "Template" in the above code always be *NULL? You're not searching 
Template, you're searching Items.  Wouldn't the following make a lot more 
sense?

                      eval  elem = (( p_match - %addr(Items) )
                                 / %size(myTemplate) ) + 1

Think about what the code is trying to accomplish.  It wants to know which 

(numeric) element was found, but all it has received from bsearch() is the 

spot in memory where the matching element is stored.

So how can it derive the element number from that?

Think about how the array is stored.  If Items(1) (the first element of 
the Items array) is stored in memory at address 12345, and each element is 

25 bytes long, then Items(2) is stored at 12370. (12345 + 25).
Items(3) is at 12395, and so on.

So, if bsearch() returns the address of the 3rd element, it'll return 
12395... that'll be the number stored in the P_match variable.  To figure 
out the element number, you can subtract the start (12345) from the 
p_Match variable to get the number of bytes between them.  (in this 
example, 50.)  Then you can divide that by the size of each element to 
figure out how many elements that number of bytes represents.  (50 / 25 = 
2).  Then add 1 to get the element number (2 + 1 = 3, the 3rd element)

It doesn't make sense to subtract "TEMPLATE" from p_Match because you 
weren't searching TEMPLATE, you were searching ITEMS.  What you really 
wanted to do was subtract the starting address of the ITEMS array from the 

address of the elemnt that it found, divide by the size of each element, 
and then add 1.

and that's how you come up with this equasion:

                      eval  elem = (( p_match - %addr(Items) )
                                 / %size(myTemplate) ) + 1

Since "Items" is a direct copy of "myTemplate" they should be the same 
size. So the following code is equivalent to the above code:

                      eval  elem = (( p_match - %addr(Items) )
                                 / %size(Items) ) + 1

So why use %size(myTemplate)?

Because, I always forgot how %size() works when you use it with an array. 
To my way of thinking, %size(Items) should be the whole array, whereas 
%size(Items(2)) should give you the size of one element.  After all, 
%addr(Items) gives you the address of the whole array, whereas 
%addr(Items(2)) gives you the address of the second element, why should 
%size() work differently?  I don't know, but it does.

In fact, %size(Items) is the size of ONE ELEMENT of the array, and you 
need to use %size(Items:*ALL) to get the size of the whole array.  I think 

that's confusing.

Consequently, I'll typically use %size(myTemplate) since myTemplate isn't 
an array, and I don't have to remember how %size() works!

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.