i'm a relative PERL newbie looking to tap the wisdom of the monks for the first time. i have working code which:
1. reads in a .csv file with the Text::CSV_XS module
2. sets the columns to friendly variable names for later placement in a here doc
3. performs some string formatting and variable setting based on values in certain columns
4. for each line in the csv, generates a predictable XML doc structure using a simple here doc layout and inserts the field values into the required XML elements
5. outputs a physical XML file to an output directory with a file name which cooresponds to the "order_id" field for each row in the input .csv
input csv file looks like:
order_id, name, item_id, item_desc, price
1, bill smith, 11, red shoes, 39.99
2, john doe, 32, black hat, 21.59
3, jane lee, 12, green shoes, 29.99
2, john doe, 11, red shoes, 39.99
the xml file output for "order_id = 1" should look like:
<order>
<order_id>1</order_id>
<name>bill smith</name>
<item>
<item_id>11</item_id>
<item_desc>red shoes</item_desc>
<price>39.99</price>
</item>
</order>
the xml file output for "order_id = 2" should look like:
<order>
<order_id>2</order_id>
<name>john doe</name>
<item>
<item_id>32</item_id>
<item_desc>black hat</item_desc>
<price>21.59</price>
</item>
<item>
<item_id>11</item_id>
<item_desc>red shoes</item_desc>
<price>39.99</price>
</item>
</order>
in the case of rows with matching "order_id" columns i want to:
1. determine how many matching rows there are in the input .csv
2. create a for loop to output as many new child XML elements as there are duplicate rows in the .csv (i do realize looping and other logic are not allowed inside here docs so i'll need to rethink that)
3. insert only the unique column data from the subsequent matching rows into additional, duplicate child elements (<item>) in the XML document
4. output the XML file to an output directory
hopefully i've provided clear and concise information describing the challenge. thanks for taking a look and for (hopefully) sharing your observations/thoughts on a solution.