× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Below is what I ended up doing. What you see is a main program I used to
test that it will work. I know some of the statements could be combined,
but I'm a believer in KISS. I want it to be easy when I look at this again
in 18 months to know exactly what I was doing. I'm going to put it into a
service program for production.

Thanks everyone for the ideas. Although I didn't really change much from
what I started out doing. :)


// Parms into the procedure
D String S 1024A
Inz('L=19.300M=21.7700LS=19.93AS=19')
D ScnTxt S 30A Inz('AS=')
D DftNotFnd S 15P 5 Inz(-1)

// local variables in the procedure
D StrPos S 5I 0
D EndPos S 5I 0
D RtnValChr S Like(String)
D RtnVal S 15P 5
D RtnVal_Len C %Len(RtnVal)
D RtnVal_DecPos C %DecPos(RtnVal)

/FREE

// Scan for the search text
StrPos = %Scan( %Trim(ScnTxt) : String : 1);

// If not found, set default
If StrPos = *Zero;

RtnVal = DftNotFnd;

// Found, so get it
Else;

// Get start position to scan for nonnumeric
StrPos += %Len(%Trim(ScnTxt));

// Find first nonnumeric (or decimal point)
EndPos = %Check( '-,.0123456789' : String : StrPos);
If EndPos = *Zero;
EndPos = %Len(String);
EndIf;

// Get to character, then decimal field
RtnValChr = %Subst( String : StrPos : EndPos - StrPos );
Monitor;
RtnVal = %Dec(RtnValChr : RtnVal_Len : RtnVal_DecPos );
On-Error;
RtnVal = *Zero;
EndMon;

Endif;

Dump(A);

*INLR = *On;
Return;

/END-FREE


On Tue, Sep 11, 2012 at 9:10 PM, Scott Klement <rpg400-l@xxxxxxxxxxxxxxxx>wrote:

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 thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

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.