I want to find range for input using scala as given below input dataframe
Input
scala> val r_df = Seq((1,"1 to 6"),(2,"44/1 to 3")).toDF("id","range")
r_df: org.apache.spark.sql.DataFrame = [id: int, range: string]
scala> r_df.show
+---+---------+
| id| range|
+---+---------+
| 1| 1 to 6|
| 2|44/1 to 3|
+---+---------+
for loop udf
val survey_to1 = udf((data1: Int, data2: Int) => {
val arr = new ArrayBuffer[Int]()
for(i <- data1 to data2)
{
arr+= i
}
arr
})
r_df4.withColumn("new", survey_to1(col("new1"),col("new3"))).show(false)
applied above for loop udf to dataframe, it is taking only "1 to 6"
+---+---------+----+----+----+------------------+
|id |range |new1|new2|new3|new |
+---+---------+----+----+----+------------------+
|1 |1 to 6 |1 |to |6 |[1, 2, 3, 4, 5, 6]|
|2 |44/1 to 3|44/1|to |3 |null |
+---+---------+----+----+----+------------------+
Expected output
+---+---------+----+----+----+------------------+
|id |range |new1|new2|new3|new |
+---+---------+----+----+----+------------------+
|1 |1 to 6 |1 |to |6 |[1, 2, 3, 4, 5, 6]|
|2 |44/1 to 3|44/1|to |3 |[44/1,44/2,44,3] |
+---+---------+----+----+----+------------------+
question from:https://stackoverflow.com/questions/65933769/range-pattern-using-for-loop