|
Hi Jeff,
On 9/11/2012 11:56 AM, Jeff Crosby wrote:
L=19.3000A=19.43M=21.7700LS=19.93AS=19.93CB=19.1600
How would I extract the numeric data behind one of these character
interludes into a decimal field?
Personally, I'd write a routine that can loop through this string and
extract each variable and it's value. Since this is an unusual format
(proprietary to your company??) it's not likely that there's any
built-in tool for it -- you have to write the logic to meet your needs.
But, I'd do something like this:
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* getVar(): Parse a variable from a string
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P getVar B
D PI 1n
D pos 10i 0
D peVar 15a varying
D peVal 15a varying
D string 32767a varying const
D options(*varsize)
D myVar s 15a varying inz('')
D myVal s 15a varying inz('')
D ch s 1n
D state s 10i 0 inz(0)
D len s 10i 0
/free
len = %len(%trimr(string));
if (pos<0 or pos>len);
return *off;
endif;
for x = pos to len;
ch = %subst(string:x:1);
select;
when state = 0;
if ch = '=';
state = 1;
else;
myVar += ch;
endif;
when state = 1;
if %check('01234567890.-,': ch) = 0;
myVal += ch;
else;
pos = x;
leave;
endif;
endsl;
endfor;
if %len(myVar) > 0;
peVar = myVar;
peVal = myVal;
return *on;
else;
return *off;
endif;
/end-free
P E
The basic idea is that it makes it easy to spin through the string and
get the data out of it. Then you can easily hunt through the string
for
what you want.
For example, if you only wanted the 'LS' variable, you could do:
D x s 10i 0
D str s 60a
D var s 15a varying
D val s 15a varying
D result s 9p 4
str = 'L=19.3000A=19.43M=21.7700LS=19.93AS=19.93CB=19.1600';
x = 1;
dow getVar(x: var: val: str);
if var = 'LS';
result = %dec( val: 9: 4 );
endif;
enddo;
You could even wrap this last bit up into a subprocedure where you
could
pass the variable name and string in, and get a 9p4 return value (or
whatever size is appropriate) and get back your result.
Or, if you wanted to extract all of the variables from the string, for
example, into a data structure, you could do something like this:
D result ds qualified
D L 9p 4
D A 9p 4
D M 9p 4
D LS 9p 4
D AS 9p 4
D CB 9p 4
str = 'L=19.3000A=19.43M=21.7700LS=19.93AS=19.93CB=19.1600';
x = 1;
dow getVar(x: var: val: str);
select;
when var = 'L';
result.L = %dec(val: 9: 4);
when var = 'A';
result.A = %dec(val: 9: 4);
when var = 'M';
result.M = %dec(val: 9: 4);
when var = 'LS';
result.LS = %dec(val: 9: 4);
when var = 'AS';
result.AS = %dec(val: 9: 4);
when var = 'CB';
result.CB = %dec(val: 9: 4);
endsl;
enddo;
It just seems like an easy approach to the problem. And assuming
there's no "built-in" way to do parse this string format (which I can't
imagine) so you'll have to write a routine anyway, this seems like a
pretty elegant way to do it.
Just my 2 cents.
--
This is the RPG programming on the IBM i / System i (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-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.