【问题标题】:Loop through a group of indexes in groovy until value is found遍历groovy中的一组索引,直到找到值
【发布时间】:2015-07-03 22:00:46
【问题描述】:

我有一个 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 时,代码将尝试向下钻取以仅获取该元素中的某个值。例如,如果 Element[1],以“0”开头,则执行“x”,否则执行“y”。我需要能够使用相同的代码遍历所有元素(或索引),直到获得所需的信息,然后在找到该数据后退出迭代/循环。

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

【问题讨论】:

  • 你能粘贴实际的代码来编译和演示你的问题吗?
  • 即使在格式编辑之后,这也不是有效的代码
  • 对不起,蒂姆,我知道这不是有效的代码。我试图“解释”这个问题。看起来我这样做让事情变得混乱。我正在使用soapUI 并创建一个groovy 脚本,该脚本从SoapUI 中的上一步中提取特定数据。 groovy 代码不是我的问题。代码工作正常。我需要帮助来理解如何使用该代码来循环几个元素。现在,我的代码设置为仅评估一个元素(即 Element[1])。因此,它采用这条线并对其进行处理。我希望能够使用相同的代码遍历我的所有元素。
  • 如果有帮助,我当然可以发布代码(它很长,而且是针对我的工作的)。它可能包含我不允许公开发布的信息。我必须清理它以使其更通用,如果它可以帮助您更好地理解问题,我可以这样做。让我知道。感谢您的帮助。
  • @tim_yates,我尝试发布代码,但它太长了 660 个字符。我只是把它分开吗?这方面的新手,所以让我知道将它带给您的最佳方式。谢谢!

标签: loops search groovy


【解决方案1】:

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

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

【讨论】:

  • 谢谢约阿希姆!我尝试上传对我有用的解决方案,但格式已关闭。我无法让它看起来像您在上面发布的内容。不知道如何修复它以便您可以看到它。
【解决方案2】:

这是对我有用的解决方案。我根据我被告知它的设计方式给它贴上了标签

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    相关资源
    最近更新 更多