How can I build a CSV Parser without any libraries?


I have been tasked to design and build a module/function that parses
a CSV file and applies a 2-arity Fun, record by record. I don’t really know where to start, as I have never done this before.

I normally just use the phoenix command, and libraries to handle any sort of Parser. Any ideas as to where i could start, or perhaps a guide that I can follow along?

Personally I feel like the imposter Syndrome is kicking in, because it seems so hard for somehting to simple. As a newer Elixir developer this is a bit challenging.

Before the conversation gets too far, can you explain why you wouldn’t use a library?

The professor strictly said NO to libraries for the purpose of creating a new parser and not using an existing one.

Is it a really tough thing to build and test?

I honestly don’t know because I have not done it as of yet. I am looking for resources and where to began.

You should just check the RFC (or a guide) for the CSV format then.

I’d do only 2 things:

  1. Parse each line at the , separator. Look at String.split/1.
  2. …but not if the string where the , is located is surrounded by " (double quotes)

For an assignment that should be quite enough.

Yes this link this was given as a resource.

I am just not understand the assignment.

Lets say the CSV file looked like this

  1. [Tom, 28, Florida]
  2. [Dick, 42, Kansas]
  3. [Harry, 69, Nebraska]

In essence I am to loop through the arrays and separate the commas and reprint the array?
Also what is meant by “applies a 2-arity Fun” It takes two arguments if one argument is a CSV file whats the other one and array?

1 Like

@Davy_Jones_Locker The best way to start working on a problem where you don’t know the answer is to break it down into pieces, and work on each piece one at a time. To “Parse a CSV” file you need to:

  1. Read a file into a string
  2. Split that string by line into a list of strings
  3. Split each line by commas (roughly).

So let’s start with (1) and (2). Are you able to get a file read into a string? Have you tried splitting that string into lines?

1 Like

Yes I agree

  1. So I can import the CSV file into the main defmodule
  2. Then use String.split(“abc”, “”) to split the CSV into lists of strings
  3. Finally the String.split could handle the second param as a comma in quotes

My question is how can I import the CSV file into my defmod main

I think you’re looking for this:

I recommend you start out here File — Elixir v1.14.1

One age old way to write a parser by hand is recursive decent And it maps really well to the concept of functional programming. It will be a bit more involved than using String.split etc but you can handle string escape rule, potential comments, and more.