Created
January 29, 2011 17:09
-
-
Save frenchy64/801999 to your computer and use it in GitHub Desktop.
Clean way to grab a particular column of each row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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