Skip to content

Instantly share code, notes, and snippets.

@frenchy64
Created January 29, 2011 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frenchy64/801999 to your computer and use it in GitHub Desktop.
Save frenchy64/801999 to your computer and use it in GitHub Desktop.
Clean way to grab a particular column of each row
;; http://stackoverflow.com/questions/4830900/how-do-i-find-the-index-of-an-item-in-a-vector/4831170#4831170
;; CSV File:
;; first-name, last-name, phone <- headers (column names)
;; Ambrose, BS, 234333
;; Chris, Squire, 4333
;; 1. Extract to vector
;; TODO code :)
(def csv-rows ..)
;; [[ "first-name" "last-name" "phone"]
;; [ "Ambrose" "BS" 234333]
;; [ "Chris" "Squire" 4333]]
;; 2. Create indexable map from headers
(def header-index (zipmap (first csv-rows)
(iterate inc 0))
;; header-index
;; {"first-name" 0
;; "last-name" 1
;; "phone" 2}
(def get-all-columns [column-name]
(for [row (rest csv-rows)]
(-> column-name header-index row)))
;; (-> "first-name"
{"first-name" 0, "last-name" 1 .. }
["Ambrose", "BS", 234333])
;; = "Ambrose"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment