0

I need to split up the data in Seq[Array[String]] type into two Seq[Double] type items.

Sample data : ([4.0|1492168815],[11.0|1491916394],[2.0|1491812028]).

I used var action1, timestamp1 = seq.map(t => (t.split("|"))).flatten.asInstanceOf[Seq[Double]] but didn't get the results as expected. Looking out for valuable suggestions.

2
  • What's is the element of your Seq[Array[String]] ? Or could you show what is seq ? Commented Nov 27, 2017 at 10:32
  • This is what my Seq[Array[String]] looks like ([4.0|1492168815],[11.0|1491916394],[2.0|1491812028]) Commented Nov 27, 2017 at 10:41

2 Answers 2

1

Assuming your input is in format "[double1|double2]",

scala> Seq("[4.0|1492168815]","[11.0|1491916394]","[2.0|1491812028]")
res72: Seq[String] = List([4.0|1492168815], [11.0|1491916394], [2.0|1491812028])

drop [ and ], then split by \\|, | is a metacharacter in regex.

scala> res72.flatMap {_.dropRight(1).drop(1).split("\\|").toList}.map{_.toDouble}
res74: Seq[Double] = List(4.0, 1.492168815E9, 11.0, 1.491916394E9, 2.0, 1.491812028E9)

Or you can do

scala> val actTime = seq.flatMap(t => t.map(x => { val temp = x.split("\\|"); (temp(0), temp(1))}))
actTime: Seq[(String, String)] = List((4.0,1492168815), (11.0,1491916394), (2.0,1491812028))

And to separate them into two Seq[Double] you can do

scala> val action1 = actTime.map(_._1.toDouble)
action1: Seq[Double] = List(4.0, 11.0, 2.0)

scala> val timestamp1 = actTime.map(_._2.toDouble)
timestamp1: Seq[Double] = List(1.492168815E9, 1.491916394E9, 1.491812028E9)

If there could be non-double data in input, you should use Try for safer Double conversion,

scala> Seq("[4.0|1492168815]","[11.0|1491916394]","[2.0|1491812028]", "[abc|abc]")
res75: Seq[String] = List([4.0|1492168815], [11.0|1491916394], [2.0|1491812028], [abc|abc])

scala> import scala.util.Success
import scala.util.Success

scala> import scala.util.Try
import scala.util.Try

scala> res75.flatMap {_.dropRight(1).drop(1).split("\\|").toList}
            .map{d => Try(d.toDouble)}
            .collect {case Success(x) => x }
res83: Seq[Double] = List(4.0, 1.492168815E9, 11.0, 1.491916394E9, 2.0, 1.491812028E9) 
Sign up to request clarification or add additional context in comments.

Comments

0

Extract each item in the input list with regular expression groups delimited with [, | and ],

val pat = "\\[(.*)\\|(.*)\\]".r

Hence if we suppose an input such as

val xs = List("[4.0|1492168815]","[11.0|1491916394]","[2.0|1491812028]")

consider

xs.map { v => val pat(a,b) = v; (a.toDouble, b.toLong) }.unzip

where we apply the regex defined in pat onto each item of the list, tuple each group for each item and finally unzip them so that we bisect the tuples into separate collections; viz.

(List(4.0, 11.0, 2.0),List(1492168815, 1491916394, 1491812028))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.