Regis d’Aubarede wrote in post #1167356:
Nithi anand wrote in post #1167354:
Thanks for reply. Could you explain your code.
txt.split(/\r?\n/)[1…-1].
explode the string content in a array of lines, excluding
the first line (titles)
map {|line| line.split(",").map {|f| f.to_f }}.
transform each line in a array of float
each_with_object({}) { |line,h|
h[line.first] = (h[line.first]||[]) << line[1..2]
}.
create a hash, create an entry for each product id,
put in it a list of all occurences of record of same id
map { |k,v| [k]+v.each_with_object([0,0]) { |(i1,i2),a|
a[0],a[1]=a[0]+i1,a[1]+i2 }
}.
for each entry in the hash, make the sum of datas, column by
column
each_with_object([]) {|(k,i1,i2),a|
a << [k.to_i, i1, i2.to_i].join(",")
}.join("\n")
create a list of string containing hash data (hash key id,
price and count) join field with ',' , then join lines with
EOL char
if you whant get a traces of what append at each step, you can insert
this code :
tap {|o| p o}.
I have report.csv file. I have to open then I have sort
it out and print it.
txt= <<EEND
EEND
for make a response with tested code, i prefer put data and code
in same source.
so yo can do, in place of <<EEND…EEND :
txt=File.read(filename)
What if I have csv file like following
“LOTNO”,“GROUPNO”,“PARTNO”, “PRICE”,“QUANTITY”
00,11,600157,402.20,1
01,22,600187,516.69,2
02,33,600163,424.16,1
03,44,600181,492.01,3
04,55,600169,445.00,2
05,66,600175,468.96,1
06,77,600181,492.01,3
07,88,600187,516.69,2
OUT PUT SHOULD BE LIKE THIS
“LOTNO”,“GROUPNO”,“PARTNO”, “PRICE”,“QUANTITY”
00,11,600157,402.20,1
01,22,600187,516.69,4
02,33,600163,424.16,1
03,44,600181,492.01,6
04,55,600169,445.00,2
05,66,600175,468.96,1