[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

hash merge

Tim Wolak

5/13/2008 2:49:00 PM

I'm not sure if I'm using this the right way and I'm hoping you guys can
help.

I am merging two hashes together using merge and need to keep the keys
the same and subtract the difference between the two values. The keys
are account numbers and the values are the balances. Not sure why the
balances are not subtracting and creating the new value.

Thanks in advance.
Tim

class Calculate
attr_reader :sktyfuta, :sktyfutb
def initialize(sktyfuta, sktyfutb)
@sktyfuta = sktyfuta
@sktyfutb = sktyfutb
end

def data_comp
@sktyfuta.merge(@sktyfutb) { |key, old_value, new_value| old_value
- new_value }
end
end
--
Posted via http://www.ruby-....

8 Answers

Yossef Mendelssohn

5/13/2008 3:14:00 PM

0

On May 13, 9:48 am, Tim Wolak <tim.wo...@gmail.com> wrote:
> I'm not sure if I'm using this the right way and I'm hoping you guys can
> help.
>
> I am merging two hashes together using merge and need to keep the keys
> the same and subtract the difference between the two values. The keys
> are account numbers and the values are the balances. Not sure why the
> balances are not subtracting and creating the new value.
>
> Thanks in advance.
> Tim
>
> class Calculate
> attr_reader :sktyfuta, :sktyfutb
> def initialize(sktyfuta, sktyfutb)
> @sktyfuta = sktyfuta
> @sktyfutb = sktyfutb
> end
>
> def data_comp
> @sktyfuta.merge(@sktyfutb) { |key, old_value, new_value| old_value
> - new_value }
> end
> end
> --
> Posted viahttp://www.ruby-....

I'm not finding that to be the case.

>> a = {:a => 1, :b => 2}
=> {:a=>1, :b=>2}
>> b = {:b => 0, :c => 3}
=> {:c=>3, :b=>0}
>> a.merge b
=> {:c=>3, :a=>1, :b=>0}
>> a.merge(b) { |key, old, new| old - new }
=> {:c=>3, :a=>1, :b=>2}

and with your code:

>> class Calculate
>> attr_reader :sktyfuta, :sktyfutb
>> def initialize(sktyfuta, sktyfutb)
>> @sktyfuta = sktyfuta
>> @sktyfutb = sktyfutb
>> end
>>
>> def data_comp
>> @sktyfuta.merge(@sktyfutb) { |key, old_value, new_value| old_value - new_value }
>> end
>> end
=> nil
>> c = Calculate.new(a, b)
=> #<Calculate:0x579cc @sktyfutb={:c=>3, :b=>0},
@sktyfuta={:b=>2, :a=>1}>
>> c.data_comp
=> {:c=>3, :a=>1, :b=>2}

Thanks for teaching me about Hash#merge with a block, by the way.

--
-yossef

Siep Korteling

5/13/2008 3:16:00 PM

0

Tim Wolak wrote:
> I'm not sure if I'm using this the right way and I'm hoping you guys can
> help.
>
> I am merging two hashes together using merge and need to keep the keys
> the same and subtract the difference between the two values. The keys
> are account numbers and the values are the balances. Not sure why the
> balances are not subtracting and creating the new value.
>
> Thanks in advance.
> Tim
>
> class Calculate
> attr_reader :sktyfuta, :sktyfutb
> def initialize(sktyfuta, sktyfutb)
> @sktyfuta = sktyfuta
> @sktyfutb = sktyfutb
> end
>
> def data_comp
> @sktyfuta.merge(@sktyfutb) { |key, old_value, new_value| old_value
> - new_value }
> end
> end

Hash.merge will give you a new Hash, a third one. If somewhere outside
of your class you call the method data_comp, it will return this new
Hash.

a = some_instance_of Calculate.datacomp
p a

I guess you expect @sktyfuta to change, but it won't unless you tell it
to.

Regards,

Siep



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

Tim Wolak

5/13/2008 3:24:00 PM

0

Thanks for the post guys. My problem is this:

a = { 54540 => 10345, 55550 => 30555 }
b = { 54540 => 11000, 55550 => 40556 }

I need to merge these into one hash where the keys are the same so that
is fine, I need to subtract the values and the difference will be the
new key.

So I'm not sure this line is doing that I think it should do, I'm pretty
sure I may have the wrong syntax.

@sktyfuta.merge(@sktyfutb) { |key, old_value, new_value| old_value -
new_value }

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

Siep Korteling

5/13/2008 3:44:00 PM

0

Tim Wolak wrote:
> Thanks for the post guys. My problem is this:
>
> a = { 54540 => 10345, 55550 => 30555 }
> b = { 54540 => 11000, 55550 => 40556 }
>
> I need to merge these into one hash where the keys are the same so that
> is fine, I need to subtract the values and the difference will be the
> new key.
Are you sure? I'll assume you meant value
>
> So I'm not sure this line is doing that I think it should do, I'm pretty
> sure I may have the wrong syntax.
>
> @sktyfuta.merge(@sktyfutb) { |key, old_value, new_value| old_value -
> new_value }
>
> Tim

The syntax is fine,Yossef Mendelssohn actually used your class. See his
example. How do you expect it to behave?

regards,

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

Tim Wolak

5/13/2008 3:58:00 PM

0

You guys were right it is working, I was returning the wrong values.
Thanks for the help! I actual got something right :)

Thanks again
Tim

>
> The syntax is fine,Yossef Mendelssohn actually used your class. See his
> example. How do you expect it to behave?
>
> regards,
>
> Siep

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

Robert Klemme

5/13/2008 4:16:00 PM

0

On 13.05.2008 17:14, Yossef Mendelssohn wrote:

> Thanks for teaching me about Hash#merge with a block, by the way.

Not really:

irb(main):009:0> h = {1=>2}
=> {1=>2}
irb(main):010:0> h.merge(10=>20) {|*a| p a}
=> {1=>2, 10=>20}
irb(main):011:0>

The block is simply ignored.

Kind regards

robert

Adam Shelly

5/13/2008 4:52:00 PM

0

On 5/13/08, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 13.05.2008 17:14, Yossef Mendelssohn wrote:
> > Thanks for teaching me about Hash#merge with a block, by the way.
> >
>
> Not really:
>
> irb(main):009:0> h = {1=>2}
> => {1=>2}
> irb(main):010:0> h.merge(10=>20) {|*a| p a}
> => {1=>2, 10=>20}
> irb(main):011:0>
>
> The block is simply ignored.
>
Not really:

irb(main):010:0> h={1,2}
=> {1=>2}
irb(main):011:0> h.merge(10=>20){|*a| p a}
=> {1=>2, 10=>20}
irb(main):012:0> h.merge(1=>20){|*a| p a}
[1, 2, 20]
=> {1=>nil}
irb(main):013:0>

The block is only called for matching keys.

-Adam

Robert Klemme

5/13/2008 9:31:00 PM

0

On 13.05.2008 18:51, Adam Shelly wrote:
> On 5/13/08, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 13.05.2008 17:14, Yossef Mendelssohn wrote:
>>> Thanks for teaching me about Hash#merge with a block, by the way.
>>>
>> Not really:
>>
>> irb(main):009:0> h = {1=>2}
>> => {1=>2}
>> irb(main):010:0> h.merge(10=>20) {|*a| p a}
>> => {1=>2, 10=>20}
>> irb(main):011:0>
>>
>> The block is simply ignored.
>>
> Not really:
>
> irb(main):010:0> h={1,2}
> => {1=>2}
> irb(main):011:0> h.merge(10=>20){|*a| p a}
> => {1=>2, 10=>20}
> irb(main):012:0> h.merge(1=>20){|*a| p a}
> [1, 2, 20]
> => {1=>nil}
> irb(main):013:0>
>
> The block is only called for matching keys.

Amazing. Thank you for the education! I should have looked at the
documentation before making a fool of myself...

Kind regards

robert