cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a better way for this matching?

TS
New Contributor III

I have an array:

var arg = condColumnsKeys

with the elements

arg: Array[String] = Array(LOT_PREFIX, PS_NAME_BOOK_TEMPLATE_NAME, PS_NAME_PAGE_NAME, PS_NAME_FIELD_NAME)

Desired outcome is to get the string "LOT_PREFIX" and store it in var ccLotPrefix

My first attempt is

var arg = condColumnsKeys
var ccLotPrefix =
  arg match {
   case "L_PREFIX" => "L_PREFIX"
   case "LOT_PREFIX" => "LOT_PREFIX"
   case _=> "LOT_PREFIX"
 }

But I get a type mismatch as it needs to match with Array[String]

command-4310537:4: error: type mismatch;
 found   : String("L_PREFIX")
 required: Array[String]
   case "L_PREFIX" => "L_PREFIX"
        ^

So my idea is to pack it into a for loop?

var j = (r.findAllIn(condValues).count(_ == "!"))
var arg = condColumnsKeys
for(j<-(0).to(j)) {
  var ccLotPrefix =
  arg match {
   case "L_PREFIX" => "L_PREFIX"
   case "LOT_PREFIX" => "LOT_PREFIX"
   case _=> "LOT_PREFIX"
 }
}

But I still don't get rid of the type mismatch. So, after some trial/error this can be achieved by

for(i<-(0).to(i)) {
  if (arg(i) == "LOT_PREFIX")
   ccLotPrefix = "LOT_PREFIX"
  else if (arg(i) == "L_PREFIX")
   ccLotPrefix = "L_PREFIX"
}
println(ccLotPrefix)
LOT_PREFIX

But is there a better way to achieve this? Because, obviously, you don't always know what's inside the array....

0 REPLIES 0

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group