Details from one line data?

Hey,

I have a txt input file. And there is a line with the following content:
M ,00000000,A ,00000000,M ,00000000,J ,00000003,J ,00000000,A ,00000000,S ,00000000,O ,00000000,N ,00000000,D ,00000000,J ,00000000,F ,00000000,M ,00000000

I would need to create a detail record with 2 fields, label and value.
Label is one letter item from above and value is number. Is there a way to create such detail record using Action (javascript) in datamapper?

You can’t add detail records from inside a script.

You can, however, store all the values in an array and then you can use that array inside a loop to extract the values to detail records. The following code does something like that (using your sample data)

var line = data.extract(1,1000,0,1,"");'';
var allFields = line.split(/,/);
var detailTable = [];
for(var i=0;i<allFields.length;i+=2){
	detailTable.push({label:allFields[i].trim(),value:allFields[i+1].trim()});
}

After that Action step, the detailTable array contains a series of objects. So you can add a loop and an Extract step set to Javascript mode. You can then extract each field by using

detailTable[steps.currentLoopCounter-1].label;

and

detailTable[steps.currentLoopCounter-1].value;

Note: since your loop won’t actually be reading lines in the data, you will have to set its Goto step to jump to a known location (for instance, back to line 1) and you will also have to specify in the Loop properties that the maximum iterations on each line is set to detailTable.length, otherwise you’ll receive an infinite loop warning.

As for the condition driving the loop, you can do something like the following:
image

I had to use detailTable.length+1 as maximum iterations on each line. The rest works fine. Thanks a lot @Phil.