Scala从数组和嵌套数组中提取值

时间:2017-10-16 22:17:58

标签: arrays scala

我有一个数组:Array [(String,(Array [String],Int))] 我需要提取:  第一要素  最后一个元素内部数组的第3和第4个元素元素[Strings]

来自3条记录的数据:我想只显示粗体项目

  
    

数组(( 27120 ,(数组(27120,2011-12-01 09:59:17.0,2013-09-07 08:29:37.0, Dale ) , Hanson ,2578 Ingram Road,Inglewood,CA,     90309),的 8 )),           ( 92694 ,(Array(92694,2013-01-25 04:11:10.0,2013-12-04 02:31:35.0, Stacy Allbritton ,4990 Clearview Drive,     萨克拉门托,加州,94215), 2 ))           ( 40581 ,(Array(40581,2012-04-03 17:53:32.0,2013-12-10 22:46:16.0, Norman Scanlon ,312 Ocala Street,Sacramento,CA,     95761), 2 ))          )

  

数据应如下所示:

27120 8 Dale Hanson
92694 2 Stacy Allbritton
40581 2 Norman Scanlon

谢谢!

1 个答案:

答案 0 :(得分:1)

鉴于格式为Array[(Int, (Array[String], Int))]的数据,您必须映射数组并提取所需的信息。

您的数据结构如下

Tuple(integer, Tuple(Array, integer)))
         |      |     |        |
         |      |     |        |
        _1     _2     |        |
                    _2._1    _2._2

例如,

scala> val data = Array((27120,(Array("27120", "2011-12-01 09:59:17.0", "2013-09-07 08:29:37.0", "Dale, Hanson", "2578 Ingram Road, Inglewood, CA, 90309"),8)), 
(92694,(Array("92694", "2013-01-25 04:11:10.0", "2013-12-04 02:31:35.0", "Stacy, Allbritton", "4990 Clearview Drive, Sacramento, CA, 94215"),2)), 
(40581,(Array("40581", "2012-04-03 17:53:32.0", "2013-12-10 22:46:16.0", "Norman, Scanlon", "312 Ocala Street, Sacramento, CA, 95761"),2)))
data: Array[(Int, (Array[String], Int))] = Array((27120,(Array(27120, 2011-12-01 09:59:17.0, 2013-09-07 08:29:37.0, Dale, Hanson, 2578 Ingram Road, Inglewood, CA, 90309),8)), (92694,(Array(92694, 2013-01-25 04:11:10.0, 2013-12-04 02:31:35.0, Stacy, Allbritton, 4990 Clearview Drive, Sacramento, CA, 94215),2)), (40581,(Array(40581, 2012-04-03 17:53:32.0, 2013-12-10 22:46:16.0, Norman, Scanlon, 312 Ocala Street, Sacramento, CA, 95761),2)))

scala> data.map(tuple => (tuple._1, tuple._2._2, tuple._2._1(3)))
res20: Array[(Int, Int, String)] = Array((27120,8,Dale, Hanson), (92694,2,Stacy, Allbritton), (40581,2,Norman, Scanlon))