Read field name in a table as data

Posted by Admin on 28-Apr-2011 09:05

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?

All Replies

Posted by Thomas Mercer-Hursh on 28-Apr-2011 12:18

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?

Posted by Admin on 28-Apr-2011 12:27

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

Posted by Håvard Danielsen on 28-Apr-2011 12:33

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

Posted by Admin on 28-Apr-2011 12:36

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

Posted by Thomas Mercer-Hursh on 28-Apr-2011 12:49

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.

Posted by Admin on 28-Apr-2011 12:52

Here are 2 samples:

Field001Field002Field004Field009Field010Field011Field012Field014Field020Field021Field023Field024Field026Field027Field029Field030Field033Field042Field043Field049Field051Field052Field053Field055Field062Field071
Manufacturer/Supplier   NameManufacturer/Supplier   Part NumberAlternate Part   NumberParent DUNS or   DUNS + 4Part   Description - ShortPart   Description - LongCurrency CodePrice Change (A,   C, D, N)Price UOMPrice Type -   User, or Net Price, or Discount price 1 ($)Price Break   Quantity For Discount 2Price Type -   User, or Net Price, or Discount price 2 ($)Price Break   Quantity For Discount 3Price Type -   User, or Net Price, or Discount price 3 ($)Price Break   Quantity For Discount 4Price Type -   User, or Net Price, or Discount price 4 ($)Package WeightCore Part NumberPrice Sheet Level   Effective DateQuantity of   Eaches in PackageLife Cycle Status   CodePart Number   Superceded ToPackage UOMMinimum Order   QuantityBuyer's DUNS +4Country Code
UnipointDM-715D.C. MotorsD.C. MotorsUSDAUM37.86$5,00034.90$10,00031.16$15,00030.226.62010-11-1512110TW
UnipointSTR-1001AJohnson Electric PMDJohnson Electric PMDD, 12V, CCW, 16 ToothUSDAUM38.94$5,00034.66$10,00032.10$15,00031.166.82010-11-151218TW
UnipointSTR-1002ABriggs & Stratton PMBriggs & Stratton PMDD, 12V, CCW, 16 ToothUSDAUM38.94$5,00034.66$10,00032.10$15,00031.166.82010-11-151218TW

Field001Field002Field006Field009Field010Field011Field012 Field013Field014Field020 Field021Field043Field049Field051Field053Field055Field057Field062
Manufacturer/Supplier   NameManufacturer/Supplier   Part NumberItem Level GTIN   (UPC Code)Parent DUNS or   DUNS + 4Part   Description - ShortPart   Description - LongCurrency Code Price Type - RetailPrice Change   (A, C, D, N)Price UOM Price Type - User, or Net Price, or Discount   price 1 ($)Price Sheet   Level Effective DateQuantity of   Eaches in PackageLife Cycle   Status CodePackage UOMMinimum Order   QuantityNational   Popularity CodeBuyer's DUNS +4
MEI100-1002666501001085BLOWER MOTOR/BUS CWBLOWER MOTOR/BUS CW 2SP INTGUSD 51.72AUM 24.562011-01-0112EA1W
MEI100-1004666501347176BLOWER MTR/2SP CW 3WBLOWER MTR/2SP CW 3W EXT GRDUSD 57.76AUM 25.842011-01-0112EA1D
MEI100-1008666501000965BLR.MOTOR/SCHOOL BUSBLR.MOTOR/SCHOOL BUS/2 WIREUSD 66.20AUM 32.772011-01-0112EA1S

Posted by Thomas Mercer-Hursh on 28-Apr-2011 13:03

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.

Posted by Håvard Danielsen on 28-Apr-2011 13:12

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.

This thread is closed