-1

I have two lists -

A = (("192.168.1.1","private","Linux_server","str1"), 
("192.168.1.2","private","Linux_server","str2"))

B = ("A","B")

I want following output

outputList = (("192.168.1.1","private","Linux_server","str1", "A"), 
("192.168.1.2","private","Linux_server","str2","B"))

I want to insert second list element into first list as list sequence.

Two lists size will be always same.

How do I get above output using scala??

Vishwas
  • 6,967
  • 5
  • 42
  • 69
  • How does it differ from http://stackoverflow.com/questions/27859281/how-to-combine-two-lists-using-scala ? – user5102379 Jan 10 '15 at 09:19
  • @srgfed01 - Here I want to add only one element from list B to list A with same index. i.e. add first element from list B to fist element of list A and so on. – Vishwas Jan 10 '15 at 09:21
  • 1
    Are you going to post a question about every minor variation, or are you going to learn something from al these questions and answers and do them yourself? – The Archetypal Paul Jan 10 '15 at 09:43
  • 2
    Also, you don't have two Lists but tuples. Please be clear about whether you really mean List or Tuple, the two are not the same. – The Archetypal Paul Jan 10 '15 at 09:45

1 Answers1

3

The short answer:

A = (A zip B).map({ case (x, y) => x :+ y })

Some compiling code to be more explicit:

val a = List(
  List("192.168.1.1", "private", "Linux_server", "str1"), 
  List("192.168.1.2", "private", "Linux_server", "str2")
)

val b = List("A", "B")

val c = List(
  List("192.168.1.1", "private", "Linux_server", "str1", "A"), 
  List("192.168.1.2", "private", "Linux_server", "str2", "B")
)

assert((a zip b).map({ case (x, y) => x :+ y }) == c)
Chris Martin
  • 30,334
  • 10
  • 78
  • 137