[resolved] How to do for loop in Express code edit in tMap

One Star

[resolved] How to do for loop in Express code edit in tMap

I am evaluating on Talend DI for my company for efferent Data Integration tool. 
Following is a for loop in my project that using another Data Integration tool.
==============================================
' Fill SegmentArray for each record in process, the SegmentArray is a global array variable
SegeType = Records("R1").Fields("MARKET_SEGEMENT") ' Get the SegeType from the field MARKET_SEGEMENT
SegNumber = 100
for i = 1 to SegCount
  if SegmentArray(i,1) = "" then
    if SegNumber = 100 then
      SegmentArray(i,1) = SegeType
      SegNumber = i
    end if  
  elseif SegmentArray(i,1) = SegeType then
    SegNumber = i
  end if 
next i
SegmentArray(SegNumber,2) = SegmentArray(SegNumber,2) + 1
SegmentArray(SegNumber,3) = SegmentArray(SegNumber,3) + Val(Records("R1").Fields("BILLED_AMT"))
==============================================
I need to find out how to do this kind for loop in tMap Expressiong editor.

Accepted Solutions
Highlighted
One Star

Re: [resolved] How to do for loop in Express code edit in tMap

Thank you very much. I found tJavaRow is a good component that allow me to add code for row by row transform.
tFileInputDelimited --->tMap--->tJavaRow--->tFileOutputDelimited.

All Replies
One Star

Re: [resolved] How to do for loop in Express code edit in tMap

Hi, YinZhong..
can you please explain what kind of data that you have and what do you want to achieve using more simple explanations?
this is just my guess, but maybe you can use tFlowToIterate component:
tFlowToIterate iterates on the input data and generates global variables.


Smiley Happy
One Star

Re: [resolved] How to do for loop in Express code edit in tMap

Hi Shy_Angel:
My project is to map data for bill print. The source record have a field called Market_segment. There are about 10 Market_segment code used in the data file. Other than data map, I also need to create a report that can list summery data for each Market_segment. I create a array to save these data and gathering these data when tMap process each source records. That is why I need to do a for loop in expression code editor inside tMap.
Last time you have helped me to do if statement that is very helpful to my project. This time I want to see if it is possible to do for loop inside tMap or not.
Regards
Yin Zhong
One Star

Re: [resolved] How to do for loop in Express code edit in tMap

Hi, YinZhong..
tMap is an advanced component that transforms and routes data from single or multiple sources to single or multiple destinations.
since we all here didn't how your data structures look like, what can be done is, creating a Java routine to perform the looping part, something like:
eg:
int[] values = new int;
// Loop over all the elements in the values array
for (int i=0; i<values.length; i++) {
// Do something with values, such as print it
System.out.println( values );
}
OR
eg:
// Find-Max
// Given a non-empty array of ints, returns
// the largest int value found in the array.
// (does not work with empty arrays)
public int findMax(int[] nums) {
int maxCounter = nums; // use nums as the max to start
// Look at every element, starting at 1
for (int i=1; i<nums.length; i++) {
if (nums > maxCounter) {
maxCounter = nums;
}
}
return maxCounter;
}

so no, you cannot do a loop within tMap but yes, you can do the looping either with tLoop/other related components + Java routine to do the processing (eg: creating a loop and store the data inside an array, etc..)
that's the rough idea..
hmm, maybe other members here got a better idea? Smiley Happy
Highlighted
One Star

Re: [resolved] How to do for loop in Express code edit in tMap

Thank you very much. I found tJavaRow is a good component that allow me to add code for row by row transform.
tFileInputDelimited --->tMap--->tJavaRow--->tFileOutputDelimited.