Shawn Wick
Shawn Wick

Reputation: 11

VTL Script - ForEach loop through array

#set ($goodSystems = ["one_system","two_system"....])
#foreach ($system in $all.Systems) ##this code must remain the same
#if ($project == "one_system")

Apologies if the context is hard to understand as this is sanitized for the most part. I'll try to make it make sense. The #foreach portion of this code can not be changed as this is the only way to loop through all the systems in the database. There are some systems that I don't need to access hence the #if statement. I've been using the if statement and manually making changes to the $project variable but this gets very time consuming.

I'm looking for a way to still use the #foreach loop but have the $project variable check against the $goodSystems array before executing some other code(not posted). Is this possible? Thanks in advance.

Upvotes: 0

Views: 676

Answers (1)

Claude Brisson
Claude Brisson

Reputation: 4130

Why not simply do:

#if( $goodSystems.contains($system) )

Of course, this is a quick and lazy solution, purists may say that the optimal way consists in looping only on target elements, splitting the loop if necessary, like this:

#set( $all = { 'Systems': ["one","two","three"] } ) ## testcase init
#set( $good = ["one","two"] )            ## manual input
#set( $junk = $good.retainAll($all) )    ## filter out manual input
#set( $notgood = $all.Systems ) 
#set( $junk = $notgood.removeAll($good)) ## systems with are not good

#foreach( $system in $good )
  ... ## do something for good systems
#end
#foreach( $system in $all.systems )
  ... ## do something for all systems
#end
#foreach( $system in $notgood )
  ... ## do something for systems which aren't good
#end

Upvotes: 0

Related Questions