循环遍历groovy中的一组索引,直到找到值

时间:2015-07-03 22:00:47

标签: loops search groovy

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

//Gets data from web service response<br>
Def longstring =
"0   * 549 F7 G8 H9
1    2247 F6 G4 H10
17JUN DFWPHX F7
          M7 B2 Y1"

//Splits the above into separate sentences/elements
longstring.split("\\r?\\n")
String[] Element=longstring.split("\\r?\\n")

//Print out of elements<br>
Log.info Element[1] = "0   * 549 F7 G8 H9"
Log.info Element[2] = "1    2247 F6 G4 H10"
Log.info Element [3] = "17JUN DFWPHX F7"
Log.info Element[4]= "          M7 B2 Y1"

我编写了一个groovy代码块,当提供元素ID时,代码将尝试向下钻取以获得该元素中的特定值。例如,如果元素[1],则以&#34; 0&#34;开头。然后做&#34; x&#34;事情,否则做&#34; y&#34;事情。我需要能够使用相同的代码遍历所有元素(或索引),直到我得到所需的信息,然后在找到数据后退出迭代/循环。

我不是一个时髦的专家。我已经看到了地图,循环和不同运营商的谷歌搜索结果。我的场景中没有一个是有意义的。每个元素中的文本不是列表。映射和循环似乎需要与我拥有的设置不同。如果您可以帮我解决这个问题,请尽可能简单地解释一下代码。提前感谢您的时间和专业知识。

2 个答案:

答案 0 :(得分:0)

这里有点难以理解你的需求,但我会尝试一下。 假设您希望根据行的模式执行一些代码。我在这里有一个例子可以尝试实现这个目的:

//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)
}

答案 1 :(得分:0)

这是适合我的解决方案。我根据我被告知它的设计标记了它

    //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