Question Jimmy Christian · Sep 18, 2023

Loop through an array or collection in DTL

Hello community,

I want to iterate through a defined collection/list of numbers for example 1,3,4,5 in my DTL.

Something like

While/For/Foreach (x in 1,3,45)

{

///some code

}

I am not able to find the syntax of foreach or while loop to use in the DTL Object script.

Any suggestions, please provide.

Thanks,

Jimmy Christian

Product version: IRIS 2022.1

Comments

Robert Cemper · Sep 18, 2023

OBJECTSCRIPT:
 

forx=1,3,4,5 {
     ; ///some code
 }
0
Jimmy Christian  Sep 18, 2023 to Robert Cemper

Thanks Robert. 

But is there any XML representation of the code in DTL?

I see foreach loop but not a for loop 

Thanks.

0
Ashok Kumar T · Sep 18, 2023

You can use code block in DTL to write the iterate code fo the list or general loop iteration 

for item=1,3,4,5/// code 
}
0
Jimmy Christian  Sep 18, 2023 to Ashok Kumar T

Thanks Ashok. Anyway to put it in XML representation in ENSEMBLE  DTL?

0
Ashok Kumar T  Sep 19, 2023 to Jimmy Christian

Hello @Jimmy Christian 

You can use the foreach element instead of code block if it's direct set to target and no code logic required. I have attached the sample codes below.  You can refer mr. @Sylvain Guilbaud samples as well.

XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='CSVtoHL7.Inputfile.Record' targetClass='CSVtoHL7.Test.NewClass2' targetDocType='2.5:ADT_A01' create='new' language='objectscript' >
<foreach property='source.EmailList()' key='k1' >
<assign value='source.EmailList.(k1)' property='target.ListOfEmails.(k1)' action='set' />
</foreach>
</transform>
}

DTL

output

0
Jimmy Christian  Sep 19, 2023 to Ashok Kumar T

Thanks Ashok for your time. But I am looking to specify a hardcode number(example 20 times) instead of sourc.emaillist() count. So it will loop 20 times.

And then i can look for certain index and do the coding.

0
Sylvain Guilbaud · Sep 18, 2023

Hi @Jimmy Christian,

the syntax in XML is :


XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='formation.RM.personne.Batch' targetClass='formation.msg.mysql.batch' create='new' language='objectscript' >
<foreach property='source.Records()' key='k1' >
<subtransform class='formation.transfo.personne' targetObj='target.list.(k1)' sourceObj='source.Records.(k1)' />
</foreach>
</transform>
}

0
Sylvain Guilbaud · Sep 18, 2023

Hi @Jimmy Christian 

another simpler sample :




XData DTL [ XMLNamespace = "http://www.intersystems.com/dtl" ]
{
<transform sourceClass='formation.RM.personne.Batch' targetClass='common.person' create='new' language='objectscript' >
<foreach property='source.Records()' key='k1' >
<assign value='source.Records.(k1).Nom' property='target.names.(k1)' action='set' />
</foreach>
</transform>
}
0
Jimmy Christian  Sep 19, 2023 to Sylvain Guilbaud

Thanks Sylvain . I am looking to specify a hardcode number(example 20 times) instead of 'source.Records()  count. So it will loop 20 or x amount of times.

And then i can look for certain index and do the coding.

0