Workflow Soluiton

 View Only
  • 1.  csv to array question

    Posted Jun 06, 2013 01:50 PM

    Hello -

    I have a CSV file that starts like this...

    File header info.
    
    1field1,1field2,1field3,1field4
    2field1,2field2,2field3,2field4
    3field1,3field2,3field3,3field4
    ...

    

    I want to discard the first two lines, then read each of the remaining records into an array.

    Any thoughts/ideas on the best way to do this?  Tnx in advance,

    Tom

    

    

    



  • 2.  RE: csv to array question

    Posted Jun 06, 2013 02:49 PM

    Pretty easy to do:

    Put an empty string collection on the stream with an Add New Data Element component - this will be the output/final results array. Loop through the file line-by-line using an Iterate Text File Lines component (Read File comp is optional if you use the FileDataType input method in the Iterate component - I use it below). Toss out any lines that don't have your delimiter, in this case a comma, with the Index of Text Within Text component. Use a Split Text Into Collection to split on the comma delimiter and create a collection for the line read. Then use an Add Items to Collection component to add the split-element collection to your output collection (the Add New Data Element guy).

    Here's what it looks line down below (working for me with your file example)... let me know if you have any detailed questions as far as setting up any of the components used

    stuff.png



  • 3.  RE: csv to array question

    Posted Jun 06, 2013 05:04 PM

    Hi reecardo -

    Thanks for your help on this!  I have your workflow functioning properly, so currently I have a non-complex array that has the following entries:

    1field1

    1field2

    1field3

    1field4

    2field1

    etc...

    Actually what I want, however, is for each line of the file to be a single entry in a complex array.  I've created the user-defined data type through a separate integration workflow so somehow (maybe a single or multiple mapping) I need to map the post-split components into the entries of the complex array.

    Do you have any ideas on how I can take the post-split components and map them into individual entries within a complex array?

    Thanks again for your help.  Any/all thoughts/suggestions are greatly appreciated!

    Tom



  • 4.  RE: csv to array question
    Best Answer

    Posted Jun 07, 2013 08:37 AM

    You're on the right track with the mapping... what you want to do is use a Single Value Mapping component (before the Add Items to Collection component) to map the collection elements into your complex element.

    Lets say you have a complex element type called Blah with 3 properties: field1, field2, and field3.

    Change the Add New Data Element comp in my pic above to be an array of Blah (vs an array of strings).

    Stick a Single Value Mapping before the Add Items to Collection comp. Pick Blah as the target type. Now create your mapping - each field of Blah (field1, field2, and field3) would be mapped using a ProcessMappingAssignment. The Conversion Model for the mapping would look like this:

    Start ->

    Get Item By Index on your split array to get the 0th, 1st, or 2nd item (as appropriate for field1, field2, field3) ->

    End (with the output hooked up to your Get Item By Index output)

    Now, take the output of the mapping and pass it to the Add Items to Collection comp (Add Items to Collection needs to be changed to point to an array of Blah vs an array of strings now, of course).

    Hope this helps. (NOTE: I tested this using a KeyValuePair - too lazy to create a 3 field complex type - but I still got it working for the 2 field KVP)

    d.png



  • 5.  RE: csv to array question

    Posted Jun 07, 2013 04:35 PM

    Awesome, thanks reecardo!

    Your solution worked perfectly... I didn't know the Single Value Mapping component could be configured to use a process like this... it's a powerful feature.  I actually came up with another method using the 'For Each Element in Collection' component to loop through the properties of the 'Blah' component, but it was very tedious and 'sloppy' in that it required me to have about a dozen or so components as opposed to just one with your method.  This is much tighter and solves a problem I suspect I'll encounter again with Workflow.

    Thanks again for your help.  Best, Tom