2015-07-03 62 views
0

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

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

//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]以“0”开始,那么做“x”的事情,否则做“y”的事情。我需要能够通过这个相同的代码循环遍历所有的元素(或索引),直到我拿走我需要的信息,然后在数据找到后退出迭代/循环。

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

+0

您能否粘贴编译并演示您的问题的实际代码? –

+0

即使在格式编辑之后,那不是有效的代码 –

+0

对不起Tim,我知道这不是有效的代码。我试图“解释”这个问题。看起来我这样做让事情变得困惑。我正在使用soapUI并创建一个groovy脚本,它从SoapUI中的上一步中提取特定的一段数据。 groovy代码不是我的问题。代码工作正常。我需要帮助理解如何使用该代码循环几个元素。现在,我的代码被设置为仅评估一个元素(即元素[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 
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) 
} 
+0

谢谢Joachim!我尝试上传了适用于我的解决方案,但格式设置已关闭。我无法让它看起来像你上面发布的内容。不知道如何解决它,所以你可以看到它。 –

相关问题