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.
As an Amazon Associate we earn from qualifying purchases.