I have this code which extracts data from a csv file and then reformats it so that it can be compared with another data set:
def dataExtract
dates = File.open(@filename_data).read.scan /\d{2}\/\d{2}\/\d{2}/
data_extracted = []
index = 0
dates.each do |date|
inbound_row = @data[4+(11*index)]
outbound_row = @data[6+(11*index)]
data_extracted.push [date, '4001', (inbound_row[1].gsub(/\,/,"").to_i + inbound_row[2].gsub(/\,/,"").to_i).to_s, 'AI', 'INBOUND']
data_extracted.push [date, '4090', inbound_row[3].gsub(/\,/,""), 'AI', 'INBOUND']
data_extracted.push [date, '1139', inbound_row[4].gsub(/\,/,""), 'RU STANDRD', 'INBOUND']
data_extracted.push [date, '1158', inbound_row[5].gsub(/\,/,""), 'RU STANDRD', 'INBOUND']
data_extracted.push [date, '4055', outbound_row[1].gsub(/\,/,""), 'RU PLUS', 'OUTBOUND']
data_extracted.push [date, '4055', outbound_row[2].gsub(/\,/,""), 'AR', 'OUTBOUND']
data_extracted.push [date, '1139', outbound_row[4].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
data_extracted.push [date, '1158', outbound_row[5].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
data_extracted.push [date, '4091', outbound_row[3].gsub(/\,/,""), 'RU STANDRD', 'OUTBOUND']
index += 1
end
return data_extracted
end
And here is a sample of the csv data (this is for one day. For multiple days there are chunks like this with an empty space inbetween them):
Date,BLOCK,,Wood,Miscellaneous,,Totals,MO
Monday,4055-RU,4055-AR,4091,1139,1158,,100
11/4/15,C Sort,B,C,iGPS,PECO,,
Starting,714,228,858,82,129,"2,011",
Sorted,"2,738",190,"1,110",144,228,"4,410",
Subtotal 1,"3,452",418,"1,968",226,357,"6,421",
Shipped,"2,700",0,"1,865",0,0,"4,565",
,752,418,103,226,357,"1,856",
Physical,752,418,103,226,357,"1,856",
Variance,0,0,0,0,0,0,
The only data being used in this csv file (besides the dates) are the sorted and shipped rows. Anyways, like I said, this works, it just isn’t very pretty. Is there a better way to execute the dates.each block since there is repeated info (date is in each array + inbound/outbound)?
Maybe simpler, maybe not – but hopefully enough to give you an idea on how to continue to improve it.
EDIT: the code was missing a ‘)’ on (inbound_row[idx].gsub(/\,/,””)) and (outbound_row[idx].gsub(/\,/,””)).