基于字符串的SCALA列表分区

时间:2018-06-14 10:59:54

标签: scala

我将数据收集到我的列表中,我遇到了在某个字符串之后对列表进行分区的困难。     在下面的数据中,我需要在digit=11的基础上将列表分为两部分,理想情况下它应该迭代并且遇到该列表包含digit=11的任何地方,这应该是第一个分区的最后一个元素。     我试图在scala中使用分区函数,但它只有索引号才有效,在我的情况下我需要使用字符串作为分区元素。

输入:

 ListBuffer(Mar 28 07:29:53.570873 mcuReadMsg() msg.type=0x1e-MCU_DigitInd  
    msg.status=0 seqNo=0 ctlLegNo=0 confNo=0 legNo=0 digit=0 incarnation_no=0 
    recording_vad=0 g729_channel_count=0 g722_channel_count=0
    , Mar 28 07:29:54.390835 mcuReadMsg() msg.type=0x1e-MCU_DigitInd  m 
     msg.status=0 seqNo=0 ctlLegNo=0 confNo=0 legNo=0 digit=11 incarnation_no=0 
     recording_vad=0 g729_channel_count=0 g722_channel_count=0
    , Mar 28 07:31:14.071779 mcuReadMsg() msg.type=0x1e-MCU_DigitInd msg.status=0 seqNo=0 ctlLegNo=0 confNo=1 legNo=1 digit=4 incarnation_no=0 recording_vad=0 g729_channel_count=0 g722_channel_count=0
     , Mar 28 07:31:14.951480 mcuReadMsg() msg.type=0x1e-MCU_DigitInd  
     msg.status=0 seqNo=0 ctlLegNo=0 confNo=1 legNo=1 digit=11 incarnation_no=0 recording_vad=0  g729_channel_count=0 g722_channel_count=0
       )

预期产出:

分区1:

         ListBuffer(Mar 28 07:29:53.570873 mcuReadMsg() msg.type=0x1e-MCU_DigitInd  
         msg.status=0 seqNo=0 ctlLegNo=0 confNo=0 legNo=0 digit=0 incarnation_no=0 
         recording_vad=0 g729_channel_count=0 g722_channel_count=0
         , Mar 28 07:29:54.390835 mcuReadMsg() msg.type=0x1e-MCU_DigitInd  m 
         msg.status=0 seqNo=0 ctlLegNo=0 confNo=0 legNo=0 digit=11 incarnation_no=0 
         recording_vad=0 g729_channel_count=0 g722_channel_count=0)

分区2:

      ListBuffer(Mar 28 07:31:14.071779 mcuReadMsg() msg.type=0x1e-MCU_DigitInd msg.status=0 seqNo=0 ctlLegNo=0 confNo=1 legNo=1 digit=4 incarnation_no=0 recording_vad=0 g729_channel_count=0 g722_channel_count=0
        , Mar 28 07:31:14.951480 mcuReadMsg() msg.type=0x1e-MCU_DigitInd   
        msg.status=0 seqNo=0 ctlLegNo=0 confNo=1 legNo=1 digit=11 incarnation_no=0 
        recording_vad=0  g729_channel_count=0 g722_channel_count=0
        )

代码:

     val lines = Source.fromFile(filename).getLines 
     val strList55 = ListBuffer[String]()
     val strList55_New = ListBuffer[String]()
      val regex844 =("-MCU_DigitInd").r 
       val ll1 =  for (line <- Source.fromFile(filename).getLines) {
       regex844.findFirstIn(line) match {
                                    case Some(text) => (strList55+=line+"\n")
                                    case None => null
    }
    }
     println(strList55)
     val regex844 =("digit=11").r
     val partition_List = strList55.partition 
    (regex844.findFirstIn(line) match {
      case Some(text)  => (strList_New +=line+"\n")
      case _ => false)
    }}}

1 个答案:

答案 0 :(得分:0)

我将span与head / tail结合使用,将第二个分区的第一个元素移动到第一个分区的末尾。

def span(list: ListBuffer[String]): (ListBuffer[String], ListBuffer[String]) = {
  if(list.isEmpty)
    (ListBuffer(), ListBuffer())
  else list.span(condition) match {
      case (first, ListBuffer()) => (first, ListBuffer())
      case (first, second) => (first ++ ListBuffer(second.head), second.tail)
  }
}

def partitions(list: ListBuffer[String]): List[ListBuffer[String]] = span(list) match {
  case (_, ListBuffer()) => List(list)
  case (partition, rest) => partition :: partitions(rest)
}

val input = ListBuffer("11 digit=1", "12 digit=2", "13 digit=11", "21 digit=11", "31", "32 digit=11", "41 digit=11", "51", "52")

println(partitions(input).mkString("\n"))