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
OBJECTSCRIPT:
for x=1,3,4,5 { ; ///some code }
Thanks Robert.
But is there any XML representation of the code in DTL?
I see foreach loop but not a for loop
Thanks.
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 }
Thanks Ashok. Anyway to put it in XML representation in ENSEMBLE DTL?
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
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.
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> }
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> }
With the test result below :
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.
💡 This question is considered a Key Question. More details here.