|
Hi Mark,
>
> Is it possible to look up a data structure array subfield?
>
Yes and no. Depends on what you mean.
Consider the following DS array:
D Item ds dim(30) qualified
D itemno 7P 0
D desc 30A
If you wanted to look up an item number in the array, you would not be
able to do so with the %lookup() BIF or LOOKUP op-code. The reason is
that these opcodes do not understand how to deal with a data structure...
However, it's really easy to just write a simple loop:
found = 0;
for x = 1 to %elem(Item);
if (Item.itemno = finditemno );
found = x;
leave;
endif;
endfor;
if (found = 0);
msg = 'Item not found!';
endif;
The %Lookup() BIF (and opcode) have not been enhanced to deal with data
structures, but they are capable of performing a lookup on a subfield that
itself contains an array. The following code IS valid, since SalesByMonth
is a simple array:
D Customer ds dim(500) qualified
D account 7P 0
D name 30A
D SalesByMonth 9P 2 dim(12)
found = %lookup(amount: Customer(1).SalesByMonth);
Obviously, if they wanted to add support for %lookup() to work with
arrays, they'd have to add some additional syntax to it. In the above
example, if you specified %lookup(Customer.SalesByMonth) which array would
it search? There are two arrays involved. But, it's a moot point, since
they haven't added any support for looking up data structures.
Since the %lookup() BIF does a sequential lookup (i.e. it runs through
every element, in sequence, until the right one is found) there's really
no advantage to using it instead of a FOR loop, as I demonstrated above.
The exception to that is when the data in the array is sorted, and you've
specified ASCEND or DESCEND on the D-spec. In that instance, a binary
search is performed on the data which is significantly faster.
If you need to do a binary search on a qualified DS today, you can do it
using the ILE C qsort() and bsearch() functions. They'll give you the
same performance advantages that you have with %lookup & ASCEND/DESCEND.
Of course, a user index or keyed file will also give you very fast
lookups...
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 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.