Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 在groovy中循环一组索引,直到找到值为止_Loops_Search_Groovy - Fatal编程技术网

Loops 在groovy中循环一组索引,直到找到值为止

Loops 在groovy中循环一组索引,直到找到值为止,loops,search,groovy,Loops,Search,Groovy,我有一个web服务响应,它为我提供了一个数据块(长字符串),我使用硬返回作为分隔符将其拆分为单独的元素。这给了我几个句子或元素(我想是索引),每个元素中都有几个数据值。例如: //从web服务响应获取数据 Def长串= “0*549 F7 G8 H9 12247 F6 G4 H10 DFWPHX F7年6月17日 M7 B2 Y1“ //将上述内容拆分为单独的句子/元素 长字符串拆分(\\r?\\n) 字符串[]元素=longstring.split(\\r?\\n) //打印出元素 Log

我有一个web服务响应,它为我提供了一个数据块(长字符串),我使用硬返回作为分隔符将其拆分为单独的元素。这给了我几个句子或元素(我想是索引),每个元素中都有几个数据值。例如:

//从web服务响应获取数据
Def长串= “0*549 F7 G8 H9 12247 F6 G4 H10 DFWPHX F7年6月17日 M7 B2 Y1“ //将上述内容拆分为单独的句子/元素 长字符串拆分(\\r?\\n) 字符串[]元素=longstring.split(\\r?\\n) //打印出元素
Log.info元素[1]=“0*549 F7 G8 H9” Log.info元素[2]=“1 2247 F6 G4 H10” Log.info元素[3]=“2017年6月DFWPHX F7” Log.info元素[4]=“M7 B2 Y1”
我已经编写了一段groovy代码,当提供元素ID时,代码将尝试向下钻取以仅获取该元素中的某个值。例如,如果元素[1]以“0”开头,则执行“x”操作,否则执行“y”操作。我需要能够使用相同的代码循环所有元素(或索引),直到我得到所需的信息,然后在找到数据后退出迭代/循环。


我不是一个优秀的专家。我已经看到了地图、循环和不同操作符的谷歌搜索结果。这些都不符合我的设想。每个元素中的文本不是列表。映射和循环似乎需要不同于我的设置。如果你能帮我解决这个问题,请尽可能简单地解释代码。提前感谢您的时间和专业知识。

这里很难理解您的需求,但我会尝试一下。 假设您希望根据行的模式执行某段代码。我举了一个例子,你可以尝试做到这一点:

//Here I define 2 action closures - what I want to happen
def whenZero = { line ->
  println "The line '$line' starts with a zero"
}
def whenOne = {line ->
  println "The line '$line' starts with a one"
}

//Then I declare patterns and map them to each of the actions
Map methodMap = ['0.*':whenZero, '1.*':whenOne]

//This method will do the matching of the pattern and call any action
def executeBasedOnKey(Map map, String line){
  map.each{ key, method -> 
    if (line.matches(key)) method(line)
  }
}

//Your input
def longstring ="""0   * 549 F7 G8 H9
1    2247 F6 G4 H10
17JUN DFWPHX F7
          M7 B2 Y1"""

//This line calls the evaluation for each of the lines
longstring.split("\\r?\\n").each{line->
  executeBasedOnKey(methodMap, line)
}

这就是对我有效的解决方案。我根据别人告诉我它是如何设计的,给它贴上标签

    //String that needs to be split
    def longString ="""0   * 549 F7 G8 H9
    1    2247  F6 G4 H10
    17JUN DFWPHX F7
           M7 B2 Y1"""


        //Splits the entire string response above into substrings based upon hard returns ("S" becomes the new chopped up strings)
    longString.split("\\r?\\n")
    String[] S=longString.split("\\r?\\n")

        //Creates the variable foundData for the loop to use below
    String foundData;


        //Creates "subS" variable for all the elements seen in the array "S" from above                                            
    for(String subS: S)                                                         
    {                   
                         //Creates a matcher pattern to look in each subelement (subS) where it looks for a regex pattern.
                         //Description of the regex used: /\d\s+(\d+).*/ = one digit followed by one or more spaces, followed by another one or more digits
                         //Note that the regex above includes: (\d+). This creates a "group" within the pattern, which is referred to below in the DataMatcher 
        def DataMatcher = (subS =~ /\d\s+(\d+).*/);                                                                                           

            //If the subelement (subS) matches the above pattern, it goes into the below block of code
        if (DataMatcher.matches())                                                
            {           //Sets the variable foundData (see above) to value of the DataMatcher and only grabs the data needed
                        //Note that the flightMatcher has two sections [0] and [1]. These represent the following:
                       //[0] = The entire string should be looked at
                       //[1] = Only group 1 in that string should be retrieved *See group created (in comments)  in the regex portion above
                  foundData = DataMatcher[0][1]; 
                       //This breaks the loop once the Data needed has been matched                                                 
                                break;                                                                                                                                                              
            }                             
    }

    //Displays the foundData needed
    log.info foundData

你能粘贴编译和演示你的问题的实际代码吗?即使在格式化编辑之后,那也是无效的代码。对不起,蒂姆,我知道这是无效的代码。我试图“解释”这个问题。看起来我这样做让事情变得混乱了。我正在使用soapUI并创建一个groovy脚本,用于从soapUI中的前一个步骤中提取特定的数据段。groovy代码不是我的问题。代码运行良好。我需要帮助理解如何使用该代码循环几个元素。现在,我的代码设置为只计算一个元素(即元素[1])。因此,它接受了这条线,并用它做了一些事情。我希望能够使用相同的代码循环我的所有元素。如果有帮助,我当然可以发布代码(代码非常长,并且特定于我的工作)。它可能包含我不允许公开发布的信息。我必须清理它,使它更通用,如果它能帮助你更好地理解这个问题,我可以这样做。请告诉我。谢谢你的帮助。@tim_yates,我试过发布代码,但是代码太长了660个字符。我要把它分开吗?新的,所以让我知道最好的方法,让它给你。谢谢谢谢你,约阿希姆!我尝试上传对我有效的解决方案,但格式设置已关闭。我不能让它看起来像你上面贴的那样。不知道如何修复它,以便您可以看到它。
    //String that needs to be split
    def longString ="""0   * 549 F7 G8 H9
    1    2247  F6 G4 H10
    17JUN DFWPHX F7
           M7 B2 Y1"""


        //Splits the entire string response above into substrings based upon hard returns ("S" becomes the new chopped up strings)
    longString.split("\\r?\\n")
    String[] S=longString.split("\\r?\\n")

        //Creates the variable foundData for the loop to use below
    String foundData;


        //Creates "subS" variable for all the elements seen in the array "S" from above                                            
    for(String subS: S)                                                         
    {                   
                         //Creates a matcher pattern to look in each subelement (subS) where it looks for a regex pattern.
                         //Description of the regex used: /\d\s+(\d+).*/ = one digit followed by one or more spaces, followed by another one or more digits
                         //Note that the regex above includes: (\d+). This creates a "group" within the pattern, which is referred to below in the DataMatcher 
        def DataMatcher = (subS =~ /\d\s+(\d+).*/);                                                                                           

            //If the subelement (subS) matches the above pattern, it goes into the below block of code
        if (DataMatcher.matches())                                                
            {           //Sets the variable foundData (see above) to value of the DataMatcher and only grabs the data needed
                        //Note that the flightMatcher has two sections [0] and [1]. These represent the following:
                       //[0] = The entire string should be looked at
                       //[1] = Only group 1 in that string should be retrieved *See group created (in comments)  in the regex portion above
                  foundData = DataMatcher[0][1]; 
                       //This breaks the loop once the Data needed has been matched                                                 
                                break;                                                                                                                                                              
            }                             
    }

    //Displays the foundData needed
    log.info foundData