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.
Happy Friday, everyone! I have a couple of new flat files coming in with numeric data, comma delimited, that I need to parse out and write to an externally defined file.
I've been coding the usual %scan() for the next comma, determine which field it has to be placed into, and if it's numeric, figure out how long it is, move it with %subst() to a char field with the same length then convert it to decimal with %dec().
I thought this seems pretty clunky and I was wondering if anyone has come up with a slick way of handling this type of conversion. The numeric field can be anywhere from 1 to 15 in length. I tried the following, but the compiler doesn't like it. Too bad!
sohunt = %dec(%subst(bulkdata:s:length):length:0);
Any suggestions would be greatly appreciated! Have a good weekend, y'all!
there is an rpgxtool that bob cozzi sells that has a function that you supply it a field and it will strip out such and return you a good numeric field.
It will take like $1,234.56 and give you back a 123456 in a numeric field. I can't remember the function name but it works really well.
chartonum is the function and as I recall it returns a 15/9 field but you can always "move" (z-add, etc.) into the sort of field you are after.
You can strip the commas, currency symbols, etc using an %xlate, something like:
NewChar = %xlate(',':' ':OldChar)
Just add the extra symbols where I put the comma, and an extra space per symbol. Leave the decimal point in there. Then you convert the character field to numeric, note that the characters that were xlate'd to spaces get suppressed out. Wrap it all in a procedure and you can then:
eval NumVale = myProc(CharVal)
Thanks to everyone for the info. I was wondering, will a CPYFRMIMPF of a csv file to an externally defined file work if numeric fields are defined in the file? They are quantities, so there is no decimal place. The file will also contain dates in MM/DD/YYYY format that I'm hopin will map fine to a DATFMT(*USA) field in the externally defined file.
It would be so nice if I don't have to write a program to parse out the fields. I'm not lazy, just trying to streamline the process as much as possible.
Thanks everyone!
Chris, CPYFRMIMPF will copy the file just as you want it. It looks at the target field definitions and converts the CSV data to that format. If the data can't fit into the format, you get an exception. Numerics (packed or otherwise) work just fine.
FYI, tomorrow's RPG Report will have something on parsing CSV.
Excellent, Bob. I'll check out the report tomorrow. I thought for sure that it wouldn't work because I have a value anywhere from 0 to 2000 in the csv field, and it's defined as 15.0 in the external file and the error is coming back saying that "the data for the field is not valid for the data type in the tofile". Huuummmm.......
if it is really 0 to 2000 and it is whole numbers (not 37.5 or blank) then it will work fine using CPYFRMIMPF. Is CPYFRMIMPF blowing up or your code using %DEC? Because %DEC and %INT do not like 'blanks as zero' values.
Hi Bob. I'm just calling the CPYFRMIMPF from a command line, trying to get the csv file converted and into the externally defined file. It's not associated with any RPG program, so I'm not using %DEC. Yes, they're all whole numbers, no decimals.
do you have a constraint on the fields? I mean if they're DDS, I wonder if a COMP or RANGE keyword is specified for the field restricting the range of data in the field. CPYFRMIMPF should not be blowing up... unless you are on an early release (pre-v5r3).