theMadKing
theMadKing

Reputation: 2074

Scala Converting Each Array Element to String and Splitting

I have an array loaded in, and been playing around in the REPL but can't seem to get this to work.

My array looks like this:

record_id|string|FALSE|1|
offer_id|decimal|FALSE|1|1,1
decision_id|decimal|FALSE|1|1,1
offer_type_cd|integer|FALSE|1|1,1
promo_id|decimal|FALSE|1|1,1
pymt_method_type_cd|decimal|FALSE|1|1,1
cs_result_id|decimal|FALSE|1|1,1
cs_result_usage_type_cd|decimal|FALSE|1|1,1
rate_index_type_cd|decimal|FALSE|1|1,1
sub_product_id|decimal|FALSE|1|1,1
campaign_id|decimal|FALSE|1|1,1

When I run my command:

for(i <- 0 until schema.length){  
    val convert = schema(i).toString; 
    convert.split('|').drop(2); 
    println(convert);
}

It won't drop anything. It also is not splitting it on the |

Upvotes: 0

Views: 66

Answers (2)

elm
elm

Reputation: 20435

Consider also defining a lambda function for mapping each item in the array, where intermediate results are passed on with the function,

val res = schema.map(s => s.toString.split('|').drop(2))

Upvotes: 0

dcastro
dcastro

Reputation: 68750

Strings are immutable, and so split and drop don't mutate the string - they return a new one.

You need to capture the result in a new val

val split = convert.split('|').drop(2); 
println(split.mkString(" "));

Upvotes: 3

Related Questions