• There has been a recent cluster of spammers accessing BARFer accounts and posting spam. To safeguard your account, please consider changing your password. It would be even better to take the additional step of enabling 2 Factor Authentication (2FA) on your BARF account. Read more here.

Learning Ruby on Rails for a class now...

Joined
Apr 27, 2010
Location
Peninsula
Moto(s)
.
Have to open a CSV file and scan it in. It's several integers and a string corresponding to a state ID, plant ID, department ID, employee name, and a count for how many things each employee built (there's also an employee ID, but doesn't need to get used for counting anything just the department/state/plant totals). Each is separated by commas.

I can get the file to read in. I can get my program to split the file and print out, line by line, each set. But I'm not sure how to do the formatting and prioritizing for the count. For example, there are two "dept ID = 57" but they're in different plants - like, plant 34 has a dept 57, and plant 38 has a dept 57. We have to insert subtotals when something changes (ie, a department or plant). We also have to space everything out nicely.

So far, just this will print out the whole CSV file, in the correct block order with headers, just not nicely formatted.

Code:
file = File.open("widgets.csv")
puts file.readline.split("\r")
file.close

Then I have this for the headers nicely formatted but haven't put in the rest of the data yet:
Code:
file = File.open("widgets.csv", "r").readline.split("\r")

array = []
file.each do |line|
     array << line.split(",")
end

hLen = [] //header length
array[0].each do |word|
     hLen << word.length
end

printf("\n%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n\n", \
	array[0][0], array[0][1], array[0][2], array[0][3], array[0][5], array[0][4])

#counts
cdept = 0
cplant = 0
cstate = 0
ctotal = 0

state = array[1][0]
plant = array[1][1]
dept = array[1][2]
curcount = array[1][4]

#iterate through the array columns
#if dept != previous dept 
	#total for dept print
#if plant != previous plant
	#total for plant print
#if state != previous state
	#total for state print


total = dept + plant + state

It needs to print out like this:
http://www.cs.sjsu.edu/~mak/CS160/assignments/1/Assignment1.pdf
 
Do you have to print as the department/plant changes? Or can you aggregate first then print subtotals/totals after you process the entire file?

If the latter, look into using a hash instead of an array of arrays. Much easier to aggregate with that.
 
Don't know anything about Ruby on Rails.

Take the assignment printout and a sheet of notebook paper. Write out what you actually want to get from the original csv file and then how you want to store it. Pick your data type you are going to use. Figure out how you are going to separate it and then write out a plan before you even write any code. It makes things a lot easier.

Are you going to store all the similar pieces of data together when you read through the file? Are you just going to split up the data and then figure out everything in a loop when you print it out? Stuff like that. If you have specific problems with how to implement a smaller piece of the problem, it's easier to find a solution.

Looking at it, you are storing the data in a really hard way to use. Each piece of data in each line needs to be identified as belonging to that line to sort it or use it properly. Just having a couple arrays with all the data in there is not going to work very well for what you want to do.

I don't know ruby on rails, but if it uses objects or something similar I would just use a separate object for each line and then just go through the objects by whatever value they are being sorted and create the total in a variable and print it out. You could create a different variable for the final values or you could just create an object of the larger subset containing objects of each line within it and then have the totals be a separate object. But Ive been looking at javascript too long so ..
 
Last edited:
So here's what I ended up with for the night...works to get the expected output, but the math is slightly off somewhere in there.

Code:
file=File.open("widgets.csv", "r").readline.split("\r")

array = []
file.each do |line|
	array << line.split(",")
end

hLen = []
array[0].each do |word|
	hLen << word.length
end

printf("\n%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n\n", \
	array[0][0], array[0][1], array[0][2], array[0][3], array[0][5], array[0][4])

#counts
deptCount = 0
plantCount = 0
stateCount = 0
totalCount = 0

#current states
curDept = 0
curPlant = 0
curState = 0

i=1
loop do
	stateID = array[i][0]
	plantID = array[i][1]
	deptID = array[i][2]
	countString = array[i][5]
	count = countString.to_i(10)
if i == 1 then 
		deptCount = count
		plantCount = count
		stateCount = count
		totalCount = count
		curDept = deptID
		curState = stateID
		curPlant = plantID
	end
if deptID != curDept && stateID == curState && curPlant == plantID then
printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	"", "", "", "", "#{deptCount}", "TOTAL FOR DEPT " + "#{curDept}")
		curDept = deptID
		deptCount = count
	else if plantID != curPlant && stateID == curState then
printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	"", "", "", "", "#{deptCount}", "TOTAL FOR DEPT " + "#{curDept}")
		printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	"", "", "", "", "#{plantCount}", "TOTAL FOR PLANT " + "#{curPlant}")

		curDept = deptID
		curPlant = plantID
		deptCount = count
		plantCount = count
	else if stateID != curState then
printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	"", "", "", "", "#{deptCount}", "TOTAL FOR DEPT " + "#{curDept}")
		printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	"", "", "", "", "#{plantCount}", "TOTAL FOR PLANT " + "#{curPlant}")
		printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	"", "", "", "", "#{stateCount}", "TOTAL FOR STATE " + "#{curState}")

		curDept = deptID
		curPlant = plantID
		curState = stateID
		deptCount = count
		plantCount = count
		stateCount = count
	else
		deptCount += count
		plantCount += count
		stateCount += count
		totalCount += count
	end
end
end
printf("%#{hLen[0]+1}s%#{hLen[1]+1}s%#{hLen[2]+1}s%#{hLen[3]+1}s%#{hLen[5]+1}s %-s\n", \
	array[i][0], array[i][1], array[i][2], array[i][3], array[i][5], array[i][4])
	
	i+=1
end
 
To start with, you should check out the Ruby CSV class.

That's the great thing about Ruby, you've got tons of already built functionality at your fingertips.
 
cool stuff !
 
Back
Top