Forum Discussion

mogili's avatar
mogili
New Contributor
9 years ago

Simple for loop throws exception

Exception: "

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script16.groovy: 30: 3 expressions are required for the classic for loop, you gave 4 at line: 30 column: 7. File: Script16.groovy @ line 30, column 7. for ( i=1; i="m"; i++;) ^ org.codehaus.groovy.antlr.ASTParserException: 3 expressions are required for the classic for loop, you gave 4 at line: 30 column: 7. File: Script16.groovy @ line 30, column 7. at org.codehaus.groovy.antlr.AntlrParserPlugin.buildAST(AntlrParserPlugin.java:263) at org.codehaus.groovy.control.SourceUnit.convert(SourceUnit.java:272) at  ......."
 
when a simple for loop is executed as shown below:
import com.eviware.soapui.support.types.StringToStringMap 
import com.eviware.soapui.support.XmlHolder
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder( "Get Publications#Response" )
 
holder.namespaces["ns1"] = "http://www.w3.org/2005/Atom"
holder.namespaces["c"] = "http://iddn.com/ns/core"
holder.namespaces["f"] = "http://iddn.com/ns/fields"
holder.namespaces["a"] = "http://iddn.com/ns/assets"
holder.namespaces["s"] = "http://iddn.com/ns/search"
 
 
//def m = context.expand( '${Get Publications#Response#declare namespace ns1=\'http://www.w3.org/2005/Atom\'; declare namespace s=\'http://iddn/ns/search\'; //ns1:feed[1]/s:request[1]/s:options[1]/s:max-results[1]}' )
 
 
def m = holder.getNodeValue( " //ns1:feed[1]/s:request[1]/s:options[1]/s:max-results[1]}" )
 
 
//for ( i in products.values())
for ( i=1;  i="m"; i++;)
{
 
def products = holder.getNodeValue("//ns1:feed[1]/ns1:entry[i]/ns1:content[1]/s:entitlement-bundle[1]/s:c_name[1]")
 
 
log.info( products);
}

1 Reply

  • Tylhadras's avatar
    Tylhadras
    SmartBear Alumni (Retired)

    Hey mogili.

     

    Your loop syntax looks a bit off. We need to make a couple of corrections.

     

    To be able to use what's in m as a stop value we need to convert it to a int. The return value of getNodeValue is a String.

     

    This can easilty be converted using:

     

    def maxResults = Integer.parseInt(m)

    Then we must correct the loop syntax. 

    There is quite a difference between the two operators = and ==, we use = for assignment and == for equality. So we neec to change your loop to:

     

    for (i = 1; i <=maxResults; i++) {
        //Do your loop thing here
    }

    Which can be read as: Starting at i=1 and while i is less than or equal maxResult do loop stuff and for every loop increase i by one.

     

    Regards

    //Gustav Lundström