2

I have a List of String declared like this:

var re1_emoticons=""::Nil

Which I have then filled in with emoticons (Strings). I am trying to add a \\ at the beginning of each String in re1_emoticons, modifying the original var.

First attempt:

re1_emoticons.foreach(t=>"""\\""" + t)

Second attempt:

re1_emoticons.foreach(t=>re1_emoticons.indexOf(t)="""\\"""+ t)

Third attempt:

re1_emoticons.foreach(t=>re1_emoticons.indexOf(t):="""\\"""+ t)    

However still don't find the way. Is there a correct way to do this?

2 Answers 2

6

Use the map-function. It creates a new list with a function applied to each of the items. When you get the new list, you can assign it to the var you already have.

re1_emoticons = re1_emoticons.map(t => """\\""" + t)
Sign up to request clarification or add additional context in comments.

Comments

2

The problem with foreach is it does not return anything. Use map instead of foreach or use for-comprehension syntax.

re1_emoticons = for(emoticon <- re1_emoticons) yield ("""\\""" + emotican) 

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.