[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Iterating through 2 fields for one hash, or array.

Peter Bailey

2/15/2007 5:07:00 PM

Hello,
I'm using Jamey Cribbs' 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.

--
Posted via http://www.ruby-....

6 Answers

Alex Young

2/15/2007 5:36:00 PM

0

Peter Bailey wrote:
> Hello,
> I'm using Jamey Cribbs' 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
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,
--
Alex

Peter Bailey

2/15/2007 7:48:00 PM

0

Alex Young wrote:
> Peter Bailey 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


--
Posted via http://www.ruby-....

Alex Young

2/15/2007 8:12:00 PM

0

Peter Bailey wrote:
> Alex Young wrote:
>> Peter Bailey 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,
>
<snip>
> @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?

--
Alex

Peter Bailey

2/16/2007 1:17:00 PM

0

Alex Young wrote:
> Peter Bailey wrote:
>>>
>>> @cost_centers = Hash.new(0)
>>> def add_to_cost_centers(center_key, amount)
>>> @cost_centers[center_key] += amount
>>> end
>>>
>>> Hope this helps,
>>
> <snip>
>> @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} . . .

--
Posted via http://www.ruby-....

Alex Young

2/16/2007 2:28:00 PM

0

Peter Bailey wrote:
> Alex Young wrote:
>> Peter Bailey wrote:
>>>> @cost_centers = Hash.new(0)
>>>> def add_to_cost_centers(center_key, amount)
>>>> @cost_centers[center_key] += amount
>>>> end
>>>>
>>>> Hope this helps,
>> <snip>
>>> @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} . . .
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.

--
Alex

Peter Bailey

2/16/2007 2:48:00 PM

0

Alex Young wrote:
> Peter Bailey 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.

--
Posted via http://www.ruby-....