2015-07-03 61 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!我嘗試上傳了適用於我的解決方案,但格式設置已關閉。我無法讓它看起來像你上面發佈的內容。不知道如何解決它,所以你可以看到它。 –

相關問題