[英]Update hash maps collection values based on another hash maps collection
I have the following hash maps collection: 我有以下哈希映射集合:
(def master-data [
{:code "1234" :name "Jhon"}
{:code "5678" :name "Brown"}
{:code "9999" :name "Steven"}
])
and I have data in csv file with this information: 并且我在csv文件中具有以下信息的数据:
old_code,new_code,old_name,new_name
1234,2222,Jhon,Travolta
4321,3456,Steven,Cody
how can i change the values of inside master-data to the new data provide by the csv file? 如何将内部主数据的值更改为csv文件提供的新数据?
the rules are: 规则是:
code
inside master-data
found in old_code
column in csv file then change code
value using new_code
value from csv file. code
内master-data
中发现的old_code
在csv文件柱然后改变code
使用值new_code
从csv文件的值。 name
inside master-data
found in old_name
column in csv file then change name
value using new_name
value from csv file. name
内部master-data
中发现的old_name
在csv文件列然后更改name
使用值new_name
从csv文件的值。 you need to map your master data, checking every item from csv and updating master data entry if needed. 您需要映射主数据,检查csv中的每个项目,并在需要时更新主数据条目。
(defn update-if [item key check new-val]
"utility function. updates a value for a key, setting it to new-val if it equals check"
(if (= (item key) check)
(assoc item key new-val)
item))
(defn update-data [data mappings]
(map (fn [item]
(reduce (fn [item [old-code new-code old-name new-name]]
(-> item
(update-if :code old-code new-code)
(update-if :name old-name new-name)))
item
mappings))
data))
in repl: 代表:
ttask.core> (def master-data [{:code "1234" :name "Jhon"}
{:code "5678" :name "Brown"}
{:code "9999" :name "Steven"}])
#'ttask.core/master-data
ttask.core> (def input "old_code,new_code,old_name,new_name
1234,2222,Jhon,Travolta
4321,3456,Steven,Cody")
#'ttask.core/input
ttask.core> (update-data master-data (rest (csv/read-csv input)))
({:code "2222", :name "Travolta"} {:code "5678", :name "Brown"} {:code "9999", :name "Cody"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.