I want to iterate through the table and create a hash, or an array, that
includes a single entry for each cost center and the total number of
pages counted for that cost center. So, I’d like to end up with
something like this below. I’m thinking I need a hash because the cost
center would be the key, with the page count being its value.
Cost Center Page Count
TMUS00 96
ER0000 42
DER000 192
Cost Center Page Count
TMUS00 96
ER0000 42
DER000 192
I don’t know the syntax for getting the data out of KirbyBase, but the
trick I usually use for this sort of thing is a hash with a default
value. Something like:
@cost_centers = Hash.new(0)
def add_to_cost_centers(center_key, amount) @cost_centers[center_key] += amount
end
Cost Center Page Count
TMUS00 96
ER0000 42
DER000 192
I don’t know the syntax for getting the data out of KirbyBase, but the
trick I usually use for this sort of thing is a hash with a default
value. Something like:
@cost_centers = Hash.new(0)
def add_to_cost_centers(center_key, amount) @cost_centers[center_key] += amount
end
Hope this helps,
Thanks, Alex. I’m sorry. I didn’t include any KirbyBase lingo
originally. Here’s a portion of my script, with your code in it. “x” is
each row of the table. With the code below, I am getting each cost
center and its respective page count, but, I’m still not getting
discrete cost centers nor page count totals. I don’t want any repeats
with the cost centers and I want a total page count for each cost
center.
@cost_centers = Hash.new(0)
def add_to_cost_centers(center_key, amount) @cost_centers[center_key] += amount
end
testcount_tbl.select(:costcenter, :pagecount).each do |x|
add_to_cost_centers(x.costcenter, x.pagecount)
puts x
end
@cost_centers = Hash.new(0)
def add_to_cost_centers(center_key, amount) @cost_centers[center_key] += amount
end
Hope this helps,
> @cost_centers = Hash.new(0)
> def add_to_cost_centers(center_key, amount)
> @cost_centers[center_key] += amount
> end
>
> testcount_tbl.select(:costcenter, :pagecount).each do |x|
> add_to_cost_centers(x.costcenter, x.pagecount)
> puts x
> end
What happens when you put:
{“CBCD00”=>8, “EDDG00”=>8, “JOSH00”=>4, “ADAM00”=>8}
{“CBCD00”=>8, “EDDG00”=>8, “JOSH00”=>4, “ADAM00”=>8, “HCCG00”=>4} . . .
Eh? You should get a single hash… What I’m trying to get at is
rather than try to access the data inside the loop, it should be exactly
what you’re looking for after the loop’s finished. You want the last
line of the pyramid, I think.
@cost_centers = Hash.new(0)
def add_to_cost_centers(center_key, amount) @cost_centers[center_key] += amount
end
Hope this helps,
> @cost_centers = Hash.new(0)
> def add_to_cost_centers(center_key, amount)
> @cost_centers[center_key] += amount
> end
>
> testcount_tbl.select(:costcenter, :pagecount).each do |x|
> add_to_cost_centers(x.costcenter, x.pagecount)
> puts x
> end
What happens when you put:
p @cost_centers
after this?
I get a half pyramid of data, like this, all the way down for about 150
entries. What I want, of course, is a single column of unique cost
center entries, with page count totals for each. But, at least I’m
getting something. Thanks for your help, Alex. I’ll keep you abreast of
any progress.
{“ADAM00”=>8}
{“CBCD00”=>8, “ADAM00”=>8}
{“CBCD00”=>8, “EDDG00”=>8, “ADAM00”=>8}
{“CBCD00”=>8, “EDDG00”=>8, “JOSH00”=>4, “ADAM00”=>8}
{“CBCD00”=>8, “EDDG00”=>8, “JOSH00”=>4, “ADAM00”=>8, “HCCG00”=>4} . . .
{“CBCD00”=>8, “EDDG00”=>8, “JOSH00”=>4, “ADAM00”=>8}
{“CBCD00”=>8, “EDDG00”=>8, “JOSH00”=>4, “ADAM00”=>8, “HCCG00”=>4} . . .
Eh? You should get a single hash… What I’m trying to get at is
rather than try to access the data inside the loop, it should be exactly
what you’re looking for after the loop’s finished. You want the last
line of the pyramid, I think.
I got it, Alex. And, it was my error. I was doing a “p” before my “end.”
As soon as I put my “p” after the “end,” it worked fine. Thanks for all
your help.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.