# Iterating through 2 fields for one hash, or array

**URL:** <https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705>\
**Category:** Ruby\
**Created:** [February 15, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705 "2007-02-15T18:06:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 15, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/1 "2007-02-15T18:06:59Z")

</div>

Hello,  
I’m using Jamey C.’ KirbyBase as a database. The beauty of it is, of  
course, that it’s all pure Ruby. . . .

I need to iterate through a table with multiple fields. I’ve shown here  
a very rudimentary example of the data that I’m working with.

Cost Center Page Count  
TMUS00 64  
ER0000 32  
DER000 128  
TMUS00 32  
DER000 64  
ER0000 10

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

Thank you.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 15, 2007, 6:36pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/2 "2007-02-15T18:36:43Z")

</div>

Peter B. wrote:

> ```
> DER000 128
> 
> ```
> 
> 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,

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 15, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/3 "2007-02-15T20:47:33Z")

</div>

Alex Y. wrote:

> Peter B. wrote:
> 
> > ```
> > DER000 128
> > 
> > ```
> > 
> > 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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 15, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/4 "2007-02-15T21:13:08Z")

</div>

Peter B. wrote:

> > @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?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 16, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/5 "2007-02-16T15:27:53Z")

</div>

Peter B. wrote:

> > > def add\_to\_cost\_centers(center\_key, amount)
> 
> {“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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 16, 2007, 2:16pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/6 "2007-02-16T14:16:55Z")

</div>

Alex Y. wrote:

> Peter B. wrote:
> 
> > > @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} . . .

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [February 16, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/iterating-through-2-fields-for-one-hash-or-array/85705/7 "2007-02-16T15:48:11Z")

</div>

Alex Y. wrote:

> Peter B. wrote:
> 
> > > > def add\_to\_cost\_centers(center\_key, amount)
> > 
> > {“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.
