1

I have a Array of Strings

scala> tokens
res34: Array[String] = Array(The, value, of, your, profile, is, 234.2., You, have, potential, to, gain, 8.3, more.)

Here each of comma separated value is a String.
I want to extract numbers from this i.e. my output should be result = (234.2, 8.3) & it should be mutable so that I can read from another array and append values

What data structure should I use to achieve this?

4 Answers 4

4

Consider

import scala.util._
tokens.flatMap(s => Try( s.split("\\W+").mkString(".").toDouble ).toOption)

where we tokenize further each array string into words and append them by a dot (this ought to strip out for instance trailing dots); we convert the resulting tokens into doubles whenever possible (note Try toOption will deliver None for failed conversions). With flatMap we keep only successful conversion.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm new at Scala. What is xs here? & are we splitting individual tokens from array or entire sentence?
Used now tokens instead of xs :) We split each token in the array in an attempt to extract "words" only (alphanumeric sequences) which are glued up with dots (mkString(".")) and then we try to convert them onto doubles.
Just a query - What if one of the numbers has comma. for example, 232,332.33
Good point, for this case the comma may be removed, e.g. s.replaceAll(",","").split("\\W+").mkString(".").toDouble
3

Simply filter your array with regex

val numericRegex: String = "\\d+\\.\\d+"
tokens filter(_.matches(numericRegex))

Note that the 6'th value 232.2. is not a number. you must remove the last dot "." so it will be 232.2

If values include spaces than you have to trim them

tokens map (_.trim) filter (_.matches(numericRegex))
res28: Array[String] = Array(234.2, 8.3)

Comments

1

With Regex, you can use this code to catch all the numbers:

\d+\.?\d+

Demo: https://regex101.com/r/pZomjn/1

Comments

1

In Scala there is a syntactic sugar to compose map and filter called for comprehension. Below is a version of regex approach based on for:

val regex = ".*\\d+\\.?\\d+.*"
val nums = for {
    str <- tokens if str.matches(regex)
    numStr = str.trim.split("\\W+").mkString(".")
} yield numStr.toDouble

It gives the desired output:

nums: Array[Double] = Array(234.2, 8.3)

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.