Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:27+00:00 2026-05-23T14:12:27+00:00

From input 1: fruit, apple, cider animal, beef, burger and input 2: animal, beef,

  • 0

From input 1:

fruit, apple, cider  
animal, beef, burger

and input 2:

animal, beef, 5kg
fruit, apple, 2liter
fish, tuna, 1kg

I need to produce:

fruit, apple, cider, 2liter
animal, beef, burger, 5kg

The closest example I could get is:

object FileMerger {
def main(args : Array[String]) {
  import scala.io._
  val f1 = (Source fromFile "file1.csv" getLines) map (_.split(", *")(1))
  val f2 = Source fromFile "file2.csv" getLines
  val out = new java.io.FileWriter("output.csv")
  f1 zip f2 foreach { x => out.write(x._1 + ", " + x._2 + "\n") }
  out.close
  }
}

The problem is that the example assumes that the two CSV files contain the same number of elements and in the same order. My merged result must only contain elements that are in the first and the second file. I am new to Scala, and any help will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:12:27+00:00Added an answer on May 23, 2026 at 2:12 pm

    You need an intersection of the two files: the lines from file1 and file2 which share some criteria. Consider this through a set theory perspective: you have two sets with some elements in common, and you need a new set with those elements. Well, there’s more to it than that, because the lines aren’t really equal…

    So, let’s say you read file1, and that’s of type List[Input1]. We could code it like this, without getting into any details of what Input1 is:

    case class Input1(line: String)
    val f1: List[Input1] = (Source fromFile "file1.csv" getLines () map Input1).toList
    

    We can do the same thing for file2 and List[Input2]:

    case class Input2(line: String)
    val f2: List[Input2] = (Source fromFile "file2.csv" getLines () map Input2).toList
    

    You might be wondering why I created two different classes if they have the exact same definition. Well, if you were reading structured data, you would have two different types, so let’s see how to handle that more complex case.

    Ok, so how do we match them, since Input1 and Input2 are different types? Well, the lines are matched by keys, which, according to your code, are the first column in each. So let’s create a class Key, and conversions Input1 => Key and Input2 => Key:

    case class Key(key: String)
    def Input1IsKey(input: Input1): Key = Key(input.line split "," head) // using regex would be better
    def Input2IsKey(input: Input2): Key = Key(input.line split "," head)
    

    Ok, now that we can produce a common Key from Input1 and Input2, let’s get the intersection of them:

    val intersection = (f1 map Input1IsKey).toSet intersect (f2 map Input2IsKey).toSet
    

    So we can build the intersection of lines we want, but we don’t have the lines! The problem is that, for each key, we need to know from which line it came. Consider that we have a set of keys, and for each key we want to keep track of a value — that’s exactly what a Map is! So we can build this:

    val m1 = (f1 map (input => Input1IsKey(input) -> input)).toMap
    val m2 = (f2 map (input => Input2IsKey(input) -> input)).toMap
    

    So the output can be produced like this:

    val output = intersection map (key => m1(key).line + ", " + m2(key).line)
    

    All you have to do now is output that.

    Let’s consider some improvements on this code. First, note that the output produced above repeats the key — that’s exactly what your code does, but not what you want in the example. Let’s change, then, Input1 and Input2 to split the key from the rest of the args:

    case class Input1(key: String, rest: String)
    case class Input2(key: String, rest: String)
    

    It’s now a bit harder to initialize f1 and f2. Instead of using split, which will break all the line unnecessarily (and at great cost to performance), we’ll divide the line right the at the first comma: everything before is key, everything after is rest. The method span does that:

    def breakLine(line: String): (String, String) = line span (',' !=)
    

    Play a bit with the span method on REPL to get a better understanding of it. As for (',' !=), that’s just an abbreviated form of saying (x => ',' != x).

    Next, we need a way to create Input1 and Input2 from a tuple (the result of breakLine):

    def TupleIsInput1(tuple: (String, String)) = Input1(tuple._1, tuple._2)
    def TupleIsInput2(tuple: (String, String)) = Input2(tuple._1, tuple._2)
    

    We can now read the files:

    val f1: List[Input1] = (Source fromFile "file1.csv" getLines () map breakLine map TupleIsInput1).toList
    val f2: List[Input2] = (Source fromFile "file2.csv" getLines () map breakLine map TupleIsInput2).toList
    

    Another thing we can simplify is intersection. When we create a Map, its keys are sets, so we can create the maps first, and then use their keys to compute the intersection:

    case class Key(key: String)
    def Input1IsKey(input: Input1): Key = Key(input.key)
    def Input2IsKey(input: Input2): Key = Key(input.key)
    
    // We now only keep the "rest" as the map value
    val m1 = (f1 map (input => Input1IsKey(input) -> input.rest)).toMap
    val m2 = (f2 map (input => Input2IsKey(input) -> input.rest)).toMap
    
    val intersection = m1.keySet intersect m2.keySet
    

    And the output is computed like this:

    val output = intersection map (key => key + m1(key) + m2(key))
    

    Note that I don’t append comma anymore — the rest of both f1 and f2 start with a comma already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clear the default values from input fields using js, but all
For a website that takes input from kids we need to filter any naughty
I need to copy from input document to output document all attributes but one.
im writing a Soulver-like application with html+js. I need to customize text from input(highlight).
I have saved input from a textarea element to a TEXT column in MySQL.
When reading data from the Input file I noticed that the ¥ symbom was
Lets say that input from the user is a decimal number, ex. 5. 2155
I want to use input from a user as a regex pattern for a
My SSIS program reads as input from a .csv file. The file has about
Following on from the question asked by Mykroft Best way to handle input from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.