Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
I have a field in a customer master that contains both City and state. I want to move state to another field. My thought process is to load the address into an array, read from the last postion untill I find the first no blank field, read one more then I have the state. here is the code
D ds
D address 30 dim(1)
D cycyst 30 overlay(Address)
x = 30;
dou address(x)<> ' ';
x = x-1;
enddo;
x = x-1;
field6 = %subst(Cucyst:x:2);
I am getting a subscript out of range on the second pass (when x=29)
any ideas ?
thanks
X + 2 = 31, your field is "only" 30.
Here's a routine I wrote for you that should work fine. NOTE: The JOBLOG function from COZTOOLS is not necessary to make it work--its just there to allow you to see the results in the JOBLOG.
H BNDDIR('COZTOOLS/RPGFREE') DFTACTGRP(*NO) /include cozTools/qcpysrc,joblog D addr s 30A Inz('Chicago, IL') D state s 30A D len s 10I 0 D x s 10I 0 C move *ON *INLR /free len = %len(%trimR(addr)); for x = len DOWNTO 1; if %subst(addr:x:1) = ' ' or %subst(addr:x:1) = ','; state = %trim(%subst(addr:x) : ', '); leave; endif; endfor; if (state = ''); // Didn't find the state! joblog('Sorry, no State in %s': addr); else; joblog('STATE=%s': state); endif; /end-free
Isn't the simplest solution something along the lines of:
EVALR State = %trimr(CityState);
Assuming that the CityState field ALWAYS contains the state as last two characters. Obviously this approach isn't foolproof, if the CityState content cannot be guaranteed.