我對編程都是新手和老手 - 主要是我在工作中編寫了很多小的Perl腳本。當我想要學習Lisp時,Clojure就出來了,所以我想在不知道Java的情況下學習Clojure。這很艱難,但迄今爲止很有趣。新手在Clojure中轉換CSV文件
我見過幾個類似的問題的例子,但沒有什麼地圖可以映射到我的問題空間。是否有一種規範的方式來爲Clojure中的每個CSV文件行提取值列表?
下面是一些實際工作的Perl代碼;包括非Perlers評論:
# convert_survey_to_cartography.pl
open INFILE, "< coords.csv"; # Input format "Northing,Easting,Elevation,PointID"
open OUTFILE, "> coords.txt"; # Output format "PointID X Y Z".
while (<INFILE>) { # Read line by line; line bound to $_ as a string.
chomp $_; # Strips out each line's <CR><LF> chars.
@fields = split /,/, $_; # Extract the line's field values into a list.
$y = $fields[0]; # y = Northing
$x = $fields[1]; # x = Easting
$z = $fields[2]; # z = Elevation
$p = $fields[3]; # p = PointID
print OUTFILE "$p $x $y $z\n" # New file, changed field order, different delimiter.
}
我Clojure中推敲出了一點點,並試圖在命令式風格湊齊了它:
; convert-survey-to-cartography.clj
(use 'clojure.contrib.duck-streams)
(let
[infile "coords.csv" outfile "coords.txt"]
(with-open [rdr (reader infile)]
(def coord (line-seq rdr))
(...then a miracle occurs...)
(write-lines outfile ":x :y :z :p")))
我不希望最後一行實際工作,但它得到了重點。我在尋找的線沿線的東西:
(def values (interleave (:p :y :x :z) (re-split #"," coord)))
感謝,
比爾
'我($ x,$ y,$ z,$ p)= split /,/;' – 2009-11-17 04:42:11
好點 - TIMTOWTDI。謝謝。 – 2009-11-17 04:57:51