|
One thing I do is I don't dim the ds but I dim the subfield and use
overlays.
D ds
D MyArray 4a dim(20)
D Code1 2a overlay(MyArray)
D Code2 2a overlay(MyArray:*next)
And I can look up by either field.
Rob Berendt
--
Group Dekko Services, LLC
Dept 01.073
PO Box 2000
Dock 108
6928N 400E
Kendallville, IN 46755
http://www.dekko.com
Scott Klement <rpg400-l@xxxxxxxxxxxxxxxx>
Sent by: rpg400-l-bounces+rob=dekko.com@xxxxxxxxxxxx
10/04/2004 02:14 PM
Please respond to
RPG programming on the AS400 / iSeries <rpg400-l@xxxxxxxxxxxx>
To
RPG programming on the AS400 / iSeries <rpg400-l@xxxxxxxxxxxx>
cc
Fax to
Subject
Re: Looking Up a Data Structure Array
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...
--
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 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.