0

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 |

1
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. Commented May 29, 2015 at 15:04

2 Answers 2

3

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(" "));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I am getting a print out of just this:[Ljava.lang.String;@196f5636 [Ljava.lang.String;@5c850045 [Ljava.lang.String;@1fd4e177 [Ljava.lang.String;@511e5bf4 etc, why is that happening with the new val being created?
@theMadKing What is the expected output?
I was actually going to use .dropRight(2) and remove the 2 right peices of information between the delimiter
@theMadKing that's because split is an array of strings, e.g. Array(FALSE, 1). You might want to println(split.mkString(" ")) instead to make it easier to visualize
0

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))

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.