Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: How to combine the values from hash using 'cname', along with total and balance.

by Fletch (Bishop)
on Jan 29, 2020 at 17:38 UTC ( [id://11112031]=note: print w/replies, xml ) Need Help??


in reply to How to combine the values from hash using 'cname', along with total and balance.

Do this, but in perl.

$ cat ~/fooble.clj (require '[clojure.data.json :as j]) (def data [{"balance" 10.12 "cname" "Smart Parking"} {"balance" 10.22 "cname" "Smart Parking"} {"balance" 10.32 "cname" "Smart Parking"} {"balance" 20.12 "cname" "Highview Parking"} {"balance" 20.22 "cname" "Highview Parking"} {"balance" 20.32 "cname" "Highview Parking"} {"balance" 20.42 "cname" "Highview Parking"} {"balance" 30.12 "cname" "ParkingEye"} {"balance" 30.22 "cname" "ParkingEye"}]) (println (j/write-str (map (fn [[k v]] (merge {:cname k} v)) (reduce (fn [acc {:strs [cname balance]}] (-> acc (assoc-in [cname :balance] (+ balance (get-in acc + [cname :balance] 0))) (assoc-in [cname :total] (inc (get-in acc [cname +:total] 0))))) {} data)))) $ lein run -m clojure.main ~/fooble.clj [{"cname":"Smart Parking","balance":30.66,"total":3},{"cname":"Highvie +w Parking","balance":81.08000000000001,"total":4},{"cname":"ParkingEy +e","balance":60.34,"total":2}]

The cake is a lie.
The cake is a lie.
The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11112031]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found