Does anyone know of a way to read a field name is a table as data? I'm trying to read in a file where the first line of the file is field names. I've created a temp-table with corresponding field names. What I want to be able to do is validate the field name in the temp-table with what I've imported into my variable (see example code below):
def var v-field01 as char no-undo.
def var v-field02 as char no-undo.
def var v-field03 as char no-udno.
input from c:\test.csv.
define temp-table tt no-undo
field field001 as char
field field002 as char
field field003 as char
field field004 as char
field field005 as char
field field006 as char
index k-tt as primary unique
field001 ascending.
import delimiter ","
v-field01
v-field02
v-field03.
assign
tt.v-field01 = <data>
tt.v-field02 = <data>
tt.v-field03 = <data>
The reason I'm trying to do this is, we're getting several price file from a vendor, which may not have every field in it, but field004 will always be the retail price, field005 will be a discounted cost and field006 will always be a raw cost. On some files we'll get all 3 prices, sometimes we'll only get a raw cost or a discounted and raw cost and we'll have to calculate the retail price. I've shortened my example, the actual file will have a max of 73 fields, but most files only have 10-20 fields. Any ideas?
I'm not quite clear what your problem is. Is the field name in the data file? I.e., you have name/value pairs? If so, why not just read it and test the value of the name and assign it to the appropriate field. I.e., read into temporary variables ... often a good idea ... and then make the assignment conditionally.
If there are no names in the file, then how do you know what fields are which?
The field names are in the files, but every file (we'll be receiving several different files from the same vendor) won't necessarily contain every field. The actual field names are field001 - field075, and if a field is in the file, we need to load it. I'm looking at a much smaller sample size just to get the concept down before I start writing all the code. So, its not that i want to exclude any fields, its that I need to know what fields I've loaded in order to be able to put the data that corresponds to that field into the proper place in our ERP system. Does that clarify my question at all?
(Just as an FYI i've sent a request to the vendor to see if it would be possible for them to send every field in every file. I figure if I can get the files formatted like that, all I have to do are some null checks to know what price to load etc).
Yes, you can assign data to fields dynamically using dynamic syntax (as in not static). The following code would assign "somevalue" to field004. (Your code would of course use variables with field names instead of the hardcoded value).
buffer tt:buffer-field("field004"):buffer-value = "somevalue".
would this be acceptable syntax (where v-field01 is a char variable that has already been assigned a value):
buffer tt:buffer-field(v-field01):buffer-value = "somevalue".
Can you give us a small sample of what the data actually looks like? I think you are making this more complicated than it needs to be.
Here are 2 samples:
Field001 | Field002 | Field004 | Field009 | Field010 | Field011 | Field012 | Field014 | Field020 | Field021 | Field023 | Field024 | Field026 | Field027 | Field029 | Field030 | Field033 | Field042 | Field043 | Field049 | Field051 | Field052 | Field053 | Field055 | Field062 | Field071 |
Manufacturer/Supplier Name | Manufacturer/Supplier Part Number | Alternate Part Number | Parent DUNS or DUNS + 4 | Part Description - Short | Part Description - Long | Currency Code | Price Change (A, C, D, N) | Price UOM | Price Type - User, or Net Price, or Discount price 1 ($) | Price Break Quantity For Discount 2 | Price Type - User, or Net Price, or Discount price 2 ($) | Price Break Quantity For Discount 3 | Price Type - User, or Net Price, or Discount price 3 ($) | Price Break Quantity For Discount 4 | Price Type - User, or Net Price, or Discount price 4 ($) | Package Weight | Core Part Number | Price Sheet Level Effective Date | Quantity of Eaches in Package | Life Cycle Status Code | Part Number Superceded To | Package UOM | Minimum Order Quantity | Buyer's DUNS +4 | Country Code |
Unipoint | DM-715 | D.C. Motors | D.C. Motors | USD | A | UM | 37.86 | $5,000 | 34.90 | $10,000 | 31.16 | $15,000 | 30.22 | 6.6 | 2010-11-15 | 1 | 2 | 1 | 10 | TW | |||||
Unipoint | STR-1001A | Johnson Electric PMD | Johnson Electric PMDD, 12V, CCW, 16 Tooth | USD | A | UM | 38.94 | $5,000 | 34.66 | $10,000 | 32.10 | $15,000 | 31.16 | 6.8 | 2010-11-15 | 1 | 2 | 1 | 8 | TW | |||||
Unipoint | STR-1002A | Briggs & Stratton PM | Briggs & Stratton PMDD, 12V, CCW, 16 Tooth | USD | A | UM | 38.94 | $5,000 | 34.66 | $10,000 | 32.10 | $15,000 | 31.16 | 6.8 | 2010-11-15 | 1 | 2 | 1 | 8 | TW |
Field001 | Field002 | Field006 | Field009 | Field010 | Field011 | Field012 | Field013 | Field014 | Field020 | Field021 | Field043 | Field049 | Field051 | Field053 | Field055 | Field057 | Field062 |
Manufacturer/Supplier Name | Manufacturer/Supplier Part Number | Item Level GTIN (UPC Code) | Parent DUNS or DUNS + 4 | Part Description - Short | Part Description - Long | Currency Code | Price Type - Retail | Price Change (A, C, D, N) | Price UOM | Price Type - User, or Net Price, or Discount price 1 ($) | Price Sheet Level Effective Date | Quantity of Eaches in Package | Life Cycle Status Code | Package UOM | Minimum Order Quantity | National Popularity Code | Buyer's DUNS +4 |
MEI | 100-1002 | 666501001085 | BLOWER MOTOR/BUS CW | BLOWER MOTOR/BUS CW 2SP INTG | USD | 51.72 | A | UM | 24.56 | 2011-01-01 | 1 | 2 | EA | 1 | W | ||
MEI | 100-1004 | 666501347176 | BLOWER MTR/2SP CW 3W | BLOWER MTR/2SP CW 3W EXT GRD | USD | 57.76 | A | UM | 25.84 | 2011-01-01 | 1 | 2 | EA | 1 | D | ||
MEI | 100-1008 | 666501000965 | BLR.MOTOR/SCHOOL BUS | BLR.MOTOR/SCHOOL BUS/2 WIRE | USD | 66.20 | A | UM | 32.77 | 2011-01-01 | 1 | 2 | EA | 1 | S |
OK, so the first line is field names, the second line descriptions, and the third and following lines data. So, yes, this is much simpler. Read the first line and build yourself a map of field name to position. Read the second line and discard it or validate that map. For the third and following lines read the data and refer to the map to tell you where to put things.
The exact nature of the map will depend on the nature of the data. Given that the data is of mixed type and the contents unpredictable, I would read in whole lines and then use the map to parse the data. E.g., suppose your map was a TT with field-name, position, and type. You could then extract the fields as strings one by one from the line you have read, look at the type for that position to convert to the correct type, and then look at the field name to do the assignment. I suppose you could use dynamic fields to do that assignment, but you could also use a case statement to do the assignments, e.g.
case ttMap.FieldName:
when "field-001" then assign Target.field-001 = deValue.
etc.
Charvell wrote:
would this be acceptable syntax (where v-field01 is a char variable that has already been assigned a value):
buffer tt:buffer-field(v-field01):buffer-value = "somevalue".
Yes. That's what I meant by not using hardcoded values.