tnt400.com - AS/400 Tips And Techniques

Sponsored by news400.com

This page is a discussion on the mentioned topic.
Most of the answers are in their original posted form, including any technical/spelling/grammatical errors.
No guarantees are expressed or implied. :-)
Comments, corrections, concerns about this tip?

Got another AS/400 question? Ask it here


What's New?
See what's new at Tips-N-Tech.

AS/400 Tips-N-Tech
AS/400 tips, techniques, and FAQ. Updated frequently.
CODEPage/400
All the code samples you can eat! RPG, CL, DDS, etc. etc.
AS/400 FAQs
The official news400.com FAQs.






All AS/400 Tip Categories / AS/400 Programming / Determining Even/Odd Values in queries


Question:

I'm trying to create an open query file that sorts on a location field in my item file. Here's the problem, my location is a six digit number: aisle (XX) - rack (XX) - shelf (X) - position (X). I need to sort the file such that all the even racks within an aisle are together as are all of the odd racks within that aisle. IE. I need

01-01-1-1
01-03-1-1
01-05-1-1
...
01-02-1-1
01-04-1-1
01-06-1-1
....

Now here is the OPNQRYF I'm working with.
OPNQRYF FILE((IORINVMS)) FORMAT(IORINVCT)
        KEYFLD((IDIVSN) (IWHALS) (IWHREO) (IWHRAK) (IWHSHF) (IWHPOS)
(IITNIM))
        MAPFLD((IWHALS '%SST(IWHLOC 1 2)')
               (IWHRAK '%SST(IWHLOC 3 2)')
               (IWHSHF '%SST(IWHLOC 5 1)')
               (IWHPOS '%SST(IWHLOC 6 1)')
               (IWHREO '*MAPFLD/IWHRAK - (2 * ( *MAPFLD/IWHRAK / 2))'

what I'm trying to do here is create a field (IWHREO) that is 1 when the rack field (IWHRAK) is odd and 0 when it is even. For this to work I need IWHRAK / 2 to round down so that for example rack 5 / 2 = 2.5 needs to be 2.

Lastly, IWHLOC is the six character location field that is in the file. IWHASL, IWHRAK, are defined in the format IORINVCT as zoned 2 digit no decimal. IWHSHF, IWHPOS, IWHREO are defined as zoned 1 digit no decimal.

I'm thinking that I may have to have some sort of work field with a couple of decimal places, or perhaps I need to subtract .25 after the division so that the results will round down when the rack is odd ( 5 / 2 -.25 = 2.25) but up when the rack is even ( 6 / 2 - .25 = 2.75).

Any thoughts??? Or can someone tell me or point me to a manual that explains the precision being used here?


Answer(s):



Ok, this seems to work but I'm open to suggestions on how to improve it.
  OPNQRYF FILE((IORINVMS)) FORMAT(IORINVCT) +
          KEYFLD((IDIVSN) (IWHALS) (IWHREO) (IWHRAK) +
                 (IWHSHF) (IWHPOS) (IITNIM)) +
          MAPFLD((IWHALS '%SST(IWHLOC 1 2)') +
                 (IWHRAK '%SST(IWHLOC 3 2)') +
                 (IWHSHF '%SST(IWHLOC 5 1)') +
                 (IWHPOS '%SST(IWHLOC 6 1)') +
                 (XRAKD2 '*MAPFLD/IWHRAK / 2' *ZONED 4 2) +
                 (XRKD2R '*MAPFLD/XRAKD2' *ZONED 2 0) +
                 (IWHREO '*MAPFLD/IWHRAK - +
                  (2 * *MAPFLD/XRKD2R)') +
                )





Thank you, how in the *!%$ did you know about this operator?? I did not see it in any of the manuals that I looked at.

Thanks again, this worked perfectly.




All you need to do is have the *MAPFLD become the modulus, or remainder, after diving by two. In OPNQRYF, you can get the remainder by using // instead of / for divide: (IWHREO *MAPFLD/IWHRAK // 2)

Note that this will cause even numbers to return 0, and odd numbers to return 1 as you requested. However this sorts the even numbers ahead of the odd numbers, and your message also made it sound like you wanted racks 1,3,5,... prior to 2,4,6,...

If this is the case, you can reverse the order by something like:

(IWHREO %ABSVAL((*MAPFLD/IWHRAK // 2) - 1))

This is untested, but should get the remainder (0 or 1), subtract 1 (giving -1 or 0), then take the absolute value (giving 1 or 0). I think this will give you odd racks, then even racks.





Other tips in this category:

Click here to see all categories.

Socket Programming And Timeout Issues
Deleting Duplicate Records From A Table
What are data queues and how to use
Retrieving SMTP Name
RPGLE example for Dynamic Screen Manager API
Calling APIs from CL - with examples!
Compare two strings in RPG character by character
How to search all pgms in QCLSRC for a keyword
Retrieve Database File Description (QDBRTVFD) API
How to redirect the output to STDERR from RPG-IV
Using Multiformat Logical To Join 2 Identical File
D-Spec *LIKE DEFN
RPG: Converting Character to Decimal
Example of ILE RPG CGI Program
Handling ILE RPG Numeric Overflow
Help with Subfile Programming in RPG III
Zoned parameter in ILE RPG
What's the best way to do modulus in CL?
RPG Nesting Source Print Utility
More on changing the SIGNON screen
A silly ILE RPG question
Calling Validation List API From ILE RPG
Soft Coding Module Names
RPG Multidimensional Arrays in Action
Build a Page-Equals-Size Lookup Window
Procedures within an ILE RPG program
Break msg from RPG
Source Debugger for batch jobs
RPGLE debugging
Timing out display sessions in DDS
Building Dynamic Stored Procedures
Put Message in System Log
Dynamic RPGSQL
Randomize function for the AS/400
What's the fastest way to do a simple RPG lookup?
First time Data queue application in RPG
Print file overflow in ILE RPG
Mapping Fields To Arrays in ILE RPG
Named inidicators
STRQMQRY: comparision operator '=' isn't correct?!
DSPDTAARA to an outfile - possible?
PCL ESC codes in RPG
Sharing DB files between two AS/400s
Help: AS/400 subfiles
How do you change the signon screen?
Determining Even/Odd Values in queries
Get day of the week in RPG
Convert UPPERCASE to lowercase
RPG IV help using APIs
Help with data area API
Detecing IFS Files from RPG
SQL in a CL program
Calling AS/400 APIs from ILE RPG
CVTDAT command to convert an *MDY to a *LONGJUL
CPYSPLF Automation
Subfile Window background problem
Using IFS APIs w/ ILE RPG
Packed or unpacked fields?
QRYDFN to source and back
What does the "optimize" parm do?
Determining the calling program
Sockets in RPG?
Sorting a user space
OVRDBF and SECURE() keyword in an ILE environment
RPG record locking
RPG Differences: V2R3 to V3R2 upgrade
Getting the relative record number (RRN)
What is the longest parameter usable in RPG?
A C function that returns a string to an RPG pgm
Problems with ZADD *ZEROS
Can you highlight code in SEU?
Reusing deleted records - OK?
Is there an easy way to change edit codes?
Help - Windowed Subfiles
Logical Files and DDS
AS/400 'machine language' - MI Programming
Library lists and performance
How to use ERRSFL
Updating in CL
Record lock wait time
Message subfile problem
Physical File Joins
CL: Copying User Profiles
Commands and PARM
ILE RPG, RPG IV, vs. RPG/400
RPG and subfiles
Multiple subfiles
Field masking for passwords
SFLMODE keyword in an ILE program
Subfile size
Color coding records in a subfile
MSGLINE in windows
Controlling cursor movement on a display file
API returns error! Why?
Put an RPG Program on the Web
Variable length records in RPG
Differences Between RPG400, RPG IV, and ILE RPG


You are at a news400.com site.
Contact Us | Report Bugs | Submit Comments/Suggestions | Read Site Use Agreement | Read Privacy Policy
Copyright © 2000 Duke Communications International.
This site is best viewed with the latest versions of Netscape or Internet Explorer, 800 x 600 resolution (or higher), and at least 256 colors.
Duke Communications   NEWS/400 | 29th Street Press | Business Finance | DominoPro | Selling AS/400 Solutions | SQL Server Magazine | Windows NT Magazine